bytes is immutable. Use bytearray.

xs = bytearray(b'\x01\x02\x03')
xs.append(5)
Answer from simonzack on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ basic 'bytearray' operation (appending elements)
r/learnpython on Reddit: basic 'bytearray' operation (appending elements)
July 13, 2017 -

I'm trying to add elements from one bytearray to another. This is the source bytearray:

filedata = open("file.bin", "rb").read()
fileba = bytearray(filedata)

This is the new bytearray to be built-up from scratch:

newba = bytearray()

The problem is when I try to copy byte elements from the old to the new :

for baelement in fileba 
    newba += b'\xC0'                #This works fine!
    newba += fileba [0]             #Doesn't work - "can't concat int to bytearray"
    newba += baelement           #Doesn't work - "can't concat int to bytearray"
    newba.extend (baelement )  #Doesn't work - "'int' object is not iterable"

I'm using Python 2.7.6, but can't get it to copy bytes from one bytearray to the other.. what could be the problem?

Discussions

Append bytes
is there any way to append bytes without a udf? i would expect something like this to work (using python client 4.0.0) import aerospike ac = aerospike.client({'hosts': [('localhost', 3000)]}) ac.connect() key = ('test', None, 0) ac.put(key, bins={'bytes': b''}) ac.append(key, bin='bytes', val=b'a') ... More on discuss.aerospike.com
๐ŸŒ discuss.aerospike.com
9
0
January 18, 2021
Python 3 Building an array of bytes - Stack Overflow
I need to build a tcp frame with raw binary data, but all examples and tutorials I've found talking about bytes always involve conversion from a string, and that's not what I need. In short, I nee... More on stackoverflow.com
๐ŸŒ stackoverflow.com
arrays - How to add bytes to bytearray in Python 3.7? - Stack Overflow
But usually you don't care that ... in python, gc will collect whats left unused anyways. 2025-06-30T09:20:51.02Z+00:00 ... Save this answer. ... Show activity on this post. I recently had this problem myself and this is what worked for me. Instead of instantiating a bytearray, I just ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to append two bytes in python? - Stack Overflow
Say you have b'\x04' and b'\x00' how can you combine them as b'\x0400'? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Embedded Inventor
embeddedinventor.com โ€บ home โ€บ python bytes: append & extend, explained with examples!
Python Bytes: Append & Extend, Explained with Examples!
December 27, 2023 - The bytes class is a data structure in Python that can be used when we wish to store a collection of bytes in an ordered manner in a contiguous area of memory. The bytes class is also immutable. In this article, weโ€™ll learn how to append an element to a bytes object and how to extend a bytes object.
๐ŸŒ
Aerospike
discuss.aerospike.com โ€บ client libraries โ€บ python client
Append bytes - Python Client - Aerospike Community Forum
January 18, 2021 - is there any way to append bytes without a udf? i would expect something like this to work (using python client 4.0.0) import aerospike ac = aerospike.client({'hosts': [('localhost', 3000)]}) ac.connect() key = ('test', None, 0) ac.put(key, bins={'bytes': b''}) ac.append(key, bin='bytes', val=b'a') but it raises exception.ParamError: (-2, "Cannot concatenate 'str' and 'non-str' objects", 'src/main/client/operate.c', 1162, False) does using a one liner udf to append bytes with https://www.aer...
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-39567-page-2.html
appending to a bytearray
As mentioned, you append() bytes. Since each element from the bytearray is a byte, that should work fine. >>> b = bytearray() >>> for x in bytearray(b'abc'): ... b.append(x) ... >>> b bytearray(b'abc'
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ bytes-python
Python - bytes, bytearray Examples - Dot Net Perls
A bytearray supports many of the same operations as a list. We can append values. We can delete a value or a range of values with del.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ append-data-to-binary-file-in-python
Append Data To Binary File In Python - GeeksforGeeks
July 23, 2025 - In this example, below code appends a list of integers [1, 2, 3, 4, 5] to the binary file 'binary_file.bin'. Inside the loop, each integer is converted to a 4-byte big-endian representation using to_bytes method, and then these byte sequences are written to the file.
Find elsewhere
๐ŸŒ
Gehrcke
gehrcke.de โ€บ 2014 โ€บ 02 โ€บ concatenate-byte-strings-in-python-3
Concatenate byte strings in Python 3 โ€“ Jan-Philip Gehrcke, PhD
February 25, 2014 - In Python 3, the __add__ operator returns what we want: ... The above would be the preferred method if you want to concatenate only two byte strings.
๐ŸŒ
Guy Rutenberg
guyrutenberg.com โ€บ 2020 โ€บ 04 โ€บ 04 โ€บ fast-bytes-concatenation-in-python
Fast bytes concatenation in Python โ€“ Guy Rutenberg
April 1, 2026 - It relies on the fact that appending to lists is efficient and then ''.join can preallocate the entire needed memory and perform the copy efficiently. As you can see below, it is much more efficient than the naive implementation. Python 2.6 introduced the bytearray as an efficient mutable bytes sequence.
๐ŸŒ
Embedded Inventor
embeddedinventor.com โ€บ home โ€บ bytearray: append & extend, explained with examples!
ByteArray: Append & Extend, Explained with Examples!
December 27, 2023 - Appending is when we add an item to the end of an ordered collection ยท For example, the following bytearray has 2 bytes โ€œ0x01โ€ and โ€œ0x02โ€
๐ŸŒ
Real Python
realpython.com โ€บ python-bytearray
Python's Bytearray: A Mutable Sequence of Bytes โ€“ Real Python
July 18, 2026 - You can modify a bytearray in Python by appending, slicing, or changing individual bytes, thanks to its mutable nature.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ bytearray()
Python bytearray() - Create Byte Array
September 27, 2024 - This example initially creates a bytearray from the string "Hello" and then modifies the first two elements. The print output will still show "Hello" as it simply reaffirms the initial values. Use the .append() or .extend() methods to add more data to the bytearray.
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ how-to-append-to-bytes-in-python.html
How to append to bytes in python
In Python, bytes objects are immutable, which means you cannot modify them directly. If you want to "append" data to a bytes object, you need to create a new bytes object that includes the original bytes and the additional bytes you want to append. You can use the + operator or the bytes() ...
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ extended-data-types โ€บ python-extended-data-types-bytes-bytearrays-exercise-2.php
Python Program: Concatenating bytes objects
August 11, 2025 - Write a Python script that takes two bytes objects, concatenates them, and then prints the length of the combined bytes object.