Alex Rivera | Logout

Python: bytearray vs array

Asked 2012-08-09T12:00:40.417
28

What is the difference between array.array('B') and bytearray?

from array import array

a = array('B', 'abc')
b = bytearray('abc')

a[0] = 100
b[0] = 'd'

print a
print b

Are there any memory or speed differences? What is the preferred use case of each one?

Edit
Report

1 Answer

15

bytearray is the successor of Python 2.x's string type. It's basically the built-in byte array type. Unlike the original string type, it's mutable.

The array module, on the other hand, was created to create binary data structures to communicate with the outside world (for example, to read/write binary file formats).

Unlike bytearray, it supports all kinds of array elements. It's flexible.

So if you just need an array of bytes, bytearray should work fine. If you need flexible formats (say when the element type of the array needs to be determined at runtime), array.array is your friend.

Without looking at the code, my guess would be that bytearray is probably faster since it doesn't have to consider different element types. But it's possible that array('B') returns a bytearray.

answered 2012-08-09T12:06:59.773

Your Answer