Complete guide to Python's bytearray function covering creation, manipulation, and practical examples of mutable byte sequences.
Last modified April 11, 2025
This comprehensive guide explores Python’s bytearray function, which creates a mutable sequence of bytes. We’ll cover creation methods, manipulation techniques, and practical examples of working with binary data.
The bytearray function returns a mutable array of bytes. Unlike bytes objects, bytearray can be modified after creation.
Key characteristics: mutable sequence of integers (0-255), supports common sequence operations, and can be converted to/from bytes and strings with specified encodings.
Here are different ways to create bytearray objects using various source types and initialization methods.
create_bytearray.py
empty = bytearray() print(empty) # bytearray(b’')
nums = bytearray([65, 66, 67]) print(nums) # bytearray(b’ABC')
original = b’hello' mutable = bytearray(original) print(mutable) # bytearray(b’hello')
text = bytearray(‘Python’, ‘utf-8’) print(text) # bytearray(b’Python’)
zeros = bytearray(5) print(zeros) # bytearray(b’\x00\x00\x00\x00\x00’)
This example shows five creation methods. The first creates an empty bytearray. The second converts integers to corresponding bytes. The third copies bytes.
The fourth demonstrates string encoding conversion. The fifth creates a pre-sized bytearray filled with null bytes. All methods produce mutable objects.
This example demonstrates how to modify a bytearray by changing individual bytes, slicing, and using various methods.
modify_bytearray.py
data = bytearray(b’Hello World')
data[0] = 74 # ‘J’ print(data) # bytearray(b’Jello World')
data[6:11] = b’Python' print(data) # bytearray(b’Jello Python')
data.extend(b’!’) print(data) # bytearray(b’Jello Python!')
data.insert(5, 32) # space print(data) # bytearray(b’Jello Python!')
del data[0] print(data) # bytearray(b’ello Python!')
The example shows various mutation operations. We modify single bytes using indexing, replace slices, extend with new bytes, insert at positions, and delete bytes.
All operations happen in-place since bytearray is mutable. This differs from bytes objects which would require creating new objects for each change.
Bytearrays often interact with text encodings. This example shows conversion between strings and bytearrays with different encodings.
encodings.py
text = “Привет” utf8 = bytearray(text, ‘utf-8’) print(utf8) # bytearray(b’\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82’)
decoded = utf8.decode(‘utf-8’) print(decoded) # Привет
latin1 = bytearray(text, ’latin1’, errors=‘replace’) print(latin1) # bytearray(b’??????’)
hex_data = bytearray.fromhex(‘48656c6c6f’) print(hex_data) # bytearray(b’Hello’)
The example demonstrates encoding conversions. We create bytearrays from Unicode strings using UTF-8 and Latin-1 encodings, then decode back to strings.
The fromhex method shows creating bytearrays from hexadecimal representations. Note how different encodings handle non-ASCII characters differently.
Bytearrays are ideal for binary data processing. This example shows practical binary operations like bit manipulation and file I/O.
binary_ops.py
data = bytearray([0b01010101, 0b10101010])
data[0] &= 0b00001111 # Mask upper bits print(bin(data[0])) # 0b101
data[1] ^= 0b11111111 print(bin(data[1])) # 0b1010101
with open(‘binary.dat’, ‘wb’) as f: f.write(data)
with open(‘binary.dat’, ‘rb’) as f: loaded = bytearray(f.read())
print(loaded == data) # True
This demonstrates low-level binary operations. We perform bit masking and XOR operations directly on bytearray elements. The example also shows file I/O.
Bytearrays are particularly useful for protocols and formats where you need to modify binary data after reading it from a file or network connection.
This example compares bytearray performance with alternatives like bytes and lists for common operations.
performance.py
import timeit
def test_bytearray(): data = bytearray(1000) for i in range(len(data)): data[i] = i % 256
def test_bytes(): data = bytes(1000) # Can’t modify - would need to create new objects
def test_list(): data = [0] * 1000 for i in range(len(data)): data[i] = i % 256
print(“bytearray:”, timeit.timeit(test_bytearray, number=1000)) print(“list:”, timeit.timeit(test_list, number=1000))
The benchmark shows bytearray’s efficiency for mutable byte operations. It’s optimized for this use case compared to alternatives like lists of integers.
For read-only operations, bytes objects would be faster, but bytearray provides the best balance when mutability is required.
Use for mutable data: When you need to modify binary data after creation
Prefer bytes for constants: Immutable data should use bytes objects
Specify encodings: Always explicitly specify encodings when converting text
Consider memoryview: For zero-copy slicing of large bytearrays
Handle errors: Use error handlers when decoding uncertain data
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.