ByteArray: Append & Extend, Explained with Examples!

In this article, let us see how to use the append and extend methods with bytearray objects with the help of some examples.

ByteArray 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.

If you wish to refresh your fundamental concepts of ByteArrays, you can read our article on Bytearray here before you continue this one!

If you are a visual learner, here is a YouTube video we made on ByteArray!

Appending values to a bytearray object using the append() method

What is Appending?

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”

bytearray(b'\x01\x02')

If the above ByteArray is appended with byte: “0x03”, then this is what it would look like

bytearray(b'\x01\x02\x03')

Here’s a visual explanation:

Using the append() method

Using the append() method is pretty simple and straightforward. Let’s have a look at an example!

myArray = bytearray([1, 2])
print(myArray)
# output: bytearray(b'\x01\x02')

myArray.append(3) # adds 3 to the end of myArray
print(myArray)
# output: bytearray(b'\x01\x02\x03')

As we can see the element “3” was added to the end of our array.

Hence the syntax to use append() method is

arrayName.append(element)

Here

  • arrayName: The name of the array to which you want to append the element.
  • element: The element you want to append to the byte array.

Here’s another example where we add elements one by one to an empty bytearray

# creating an empty bytearray
my_byte_array = bytearray()

my_byte_array.append(104) #ascii code for 'h'
my_byte_array.append(105) #ascii code for 'i'
my_byte_array.append(33)  #ascii code for '!'

print(my_byte_array)
bytearray(b'hi!')

The article here explains more about how we are able to add letters with the ASCII code

Extending a bytearray object with another bytearray object using the extend() method

The extend() method is used to add all the elements of one ByteArray object to another ByteArray object.

Using the extend() function is pretty similar to how we used the append() function, here’s an example:

myArray = bytearray(b'\x01\x02\x03')
myOtherArray = bytearray([4, 5, 6])
print(myOtherArray)
# output: bytearray(b'\x04\x05\x06')

myArray.extend(myOtherArray)
print(myArray)
# output: bytearray(b'\x01\x02\x03\x04\x05\x06')

As you can see, all the elements from myOtherArray (“4”, “5”, and “6”) got added to the end of the 1st array.

In other words, myArray got extended with myOtherArray !

And here’s the syntax for the usage of extend() function:

arrayName.extend(otherArrayName)

Here

  • arrayName: The name of the array you want to extend.
  • element: The object containing the array you want to extend.

Let’s take a look at one last example of using extend()

# creating a bytearray
my_byte_array = bytearray(b'Hello ')

# extending the bytearray
my_byte_array.extend('World!'.encode()) 

print(my_byte_array)
bytearray(b'Hello World!')

And with that example, I will end this article.

Congratulations on making it to the end of the article, not many have the perseverance to do so!

I hope you enjoyed reading this article and found it useful!

Feel free to share it with your friends and colleagues!

If your thirst for knowledge has not been quenched yet, here are some related articles that might spark your interest!

Related Articles

Python: bytearray vs bytes, Similarities & Differences Explained!

Python ByteArray: Everything you need to know!

Thanks to Namazi Jamal for his contributions in writing this article!

Photo of author
Editor
Balaji Gunasekaran
Balaji Gunasekaran is a Senior Software Engineer with a Master of Science degree in Mechatronics and a bachelor’s degree in Electrical and Electronics Engineering. He loves to write about tech and has written more than 300 articles. He has also published the book “Cracking the Embedded Software Engineering Interview”. You can follow him on LinkedIn