Python’s bytearray
is a mutable sequence of bytes that allows you to manipulate binary data efficiently. Unlike immutable bytes
, bytearray
can be modified in place, making it suitable for tasks requiring frequent updates to byte sequences.
You can create a bytearray
using the bytearray()
constructor with various arguments or from a string of hexadecimal digits using .fromhex()
. This tutorial explores creating, modifying, and using bytearray
objects in Python.
By the end of this tutorial, you’ll understand that:
- A
bytearray
in Python is a mutable sequence of bytes that allows in-place modifications, unlike the immutablebytes
. - You create a
bytearray
by using thebytearray()
constructor with a non-negative integer, iterable of integers, bytes-like object, or a string with specified encoding. - You can modify a
bytearray
in Python by appending, slicing, or changing individual bytes, thanks to its mutable nature. - Common uses for
bytearray
include processing large binary files, working with network protocols, and tasks needing frequent updates to byte sequences.
You’ll dive deeper into each aspect of bytearray
, exploring its creation, manipulation, and practical applications in Python programming.
Get Your Code: Click here to download the free sample code that you’ll use to learn about Python’s bytearray data type.
Take the Quiz: Test your knowledge with our interactive “Python's Bytearray” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Python's BytearrayIn this quiz, you'll test your understanding of Python's bytearray data type. By working through this quiz, you'll revisit the key concepts and uses of bytearray in Python.
Understanding Python’s bytearray
Type
Although Python remains a high-level programming language, it exposes a few specialized data types that let you manipulate binary data directly should you ever need to. These data types can be useful for tasks such as processing custom binary file formats, or working with low-level network protocols requiring precise control over the data.
The three closely related binary sequence types built into the language are:
bytes
bytearray
memoryview
While they’re all Python sequences optimized for performance when dealing with binary data, they each have slightly different strengths and use cases.
Note: You’ll take a deep dive into Python’s bytearray
in this tutorial. But, if you’d like to learn more about the companion bytes
data type, then check out Bytes Objects: Handling Binary Data in Python, which also covers binary data fundamentals.
As both names suggest, bytes
and bytearray
are sequences of individual byte values, letting you process binary data at the byte level. For example, you may use them to work with plain text data, which typically represents characters as unique byte values, depending on the given character encoding.
Python natively interprets bytes as 8-bit unsigned integers, each representing one of 256 possible values (28) between 0 and 255. But sometimes, you may need to interpret the same bit pattern as a signed integer, for example, when handling digital audio samples that encode a sound wave’s amplitude levels. See the section on signedness in the Python bytes
tutorial for more details.
The choice between bytes
and bytearray
boils down to whether you want read-only access to the underlying bytes or not. Instances of the bytes
data type are immutable, meaning each one has a fixed value that you can’t change once the object is created. In contrast, bytearray
objects are mutable sequences, allowing you to modify their contents after creation.
While it may seem counterintuitive at first—since many newcomers to Python expect objects to be directly modifiable—immutable objects have several benefits over their mutable counterparts. That’s why types like strings, tuples, and others require reassignment in Python.
The advantages of immutable data types include better memory efficiency due to the ability to cache or reuse objects without unnecessary copying. In Python, immutable objects are inherently hashable, so they can become dictionary keys or set elements. Additionally, relying on immutable objects gives you extra security, data integrity, and thread safety.
That said, if you need a binary sequence that allows for modification, then bytearray
is the way to go. Use it when you frequently perform in-place byte operations that involve changing the contents of the sequence, such as appending, inserting, extending, or modifying individual bytes. A scenario where bytearray
can be particularly useful includes processing large binary files in chunks or incrementally reading messages from a network buffer.
The third binary sequence type in Python mentioned earlier, memoryview
, provides a zero-overhead view into the memory of certain objects. Unlike bytes
and bytearray
, whose mutability status is fixed, a memoryview
can be either mutable or immutable depending on the target object it references. Just like bytes
and bytearray
, a memoryview
may represent a series of single bytes, but at the same time, it can represent a sequence of multi-byte words.
Now that you have a basic understanding of Python’s binary sequence types and where bytearray
fits into them, you can explore ways to create and work with bytearray
objects in Python.
Creating bytearray
Objects in Python
Unlike the immutable bytes
data type, whose literal form resembles a string literal prefixed with the letter b
—for example, b"GIF89a"
—the mutable bytearray
has no literal syntax in Python. This distinction is important despite many similarities between both byte-oriented sequences, which you’ll discover in the next section.
The primary way to create new bytearray
instances is by explicitly calling the type’s class constructor, sometimes informally known as the bytearray()
built-in function. Alternatively, you can create a bytearray
from a string of hexadecimal digits. You’ll learn about both methods next.