🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Bytearray

AI & DATA SCIENCE // bytearray

bytearray is Python's built-in, mutable counterpart to bytes — a resizable sequence of raw byte values that can be modified in place.

Syntax

ba = bytearray(b"hello")
ba[0] = 72
ba.append(33)

Deep Dive Course

bytearray behaves exactly like bytes for reading — indexing, slicing, decoding — but additionally supports in-place mutation: assigning to an index, append(), extend(), and slice assignment, all without creating a new object each time. This makes it the right choice for building up or modifying binary data incrementally, such as accumulating chunks read from a socket or file.

1Understanding Bytearray

bytearray behaves exactly like bytes for reading — indexing, slicing, decoding — but additionally supports in-place mutation: assigning to an index, append(), extend(), and slice assignment, all without creating a new object each time. This makes it the right choice for building up or modifying binary data incrementally, such as accumulating chunks read from a socket or file.

💡

Use bytearray instead of repeatedly concatenating bytes objects when building up binary data piece by piece — like strings, bytes is immutable, so += in a loop is just as wasteful for it as it is for str.

editor.html
ba = bytearray(b"Hello")
ba[0] = ord("J")
print(ba)
print(ba.decode())
localhost:3000

2Practical Example

Here is a real-world application of Bytearray showing how it is used in production Python code.

editor.html
buffer = bytearray()
for chunk in [b"Hel", b"lo, ", b"World!"]:
    buffer.extend(chunk)
print(bytes(buffer))
localhost:3000

3Best Practices

Follow these guidelines when working with Bytearray:

1. Use bytearray instead of bytes when the binary data needs to be modified after creation

2. Convert to bytes with bytes(ba) once mutation is done, if you need an immutable, hashable result

3. Use bytearray.extend() to append multiple bytes at once instead of looping with individual append() calls

⚠️

Tip: Use bytearray instead of repeatedly concatenating bytes objects when building up binary data piece by piece — like strings, bytes is immutable, so += in a loop is just as wasteful for it as it is for str.

editor.html
ba = bytearray(b"Hello")
ba[0] = ord("J")
print(ba)
print(ba.decode())
localhost:3000

Examples

Example 01Basic Usage
ba = bytearray(b"Hello")
ba[0] = ord("J")
print(ba)
print(ba.decode())
Example 02Advanced Example
buffer = bytearray()
for chunk in [b"Hel", b"lo, ", b"World!"]:
    buffer.extend(chunk)
print(bytes(buffer))

Best Practices

  • Use bytearray instead of bytes when the binary data needs to be modified after creation
  • Convert to bytes with bytes(ba) once mutation is done, if you need an immutable, hashable result
  • Use bytearray.extend() to append multiple bytes at once instead of looping with individual append() calls

Interview Question

Why can you assign to an index of a bytearray, but not to an index of a bytes object?

Hint: It's the same distinction as list versus tuple, applied to binary data.

bytes is immutable by design, the binary equivalent of a fixed str, so index assignment raises a TypeError. bytearray is explicitly the mutable version of the same byte-sequence concept, implemented to support in-place item assignment and resizing, which is why it exists as a separate type alongside bytes rather than bytes simply allowing mutation.

Exercises

MediumPractice using Bytearray in a real scenario.
View Solution
ba = bytearray(b"Hello")
ba[0] = ord("J")
print(ba)
print(ba.decode())

Frequently Asked Questions

Why can you assign to an index of a bytearray, but not to an index of a bytes object?

bytes is immutable by design, the binary equivalent of a fixed str, so index assignment raises a TypeError. bytearray is explicitly the mutable version of the same byte-sequence concept, implemented to support in-place item assignment and resizing, which is why it exists as a separate type alongside bytes rather than bytes simply allowing mutation.

Related Functions

bytesstringsmemoryview