Converting a list of integers in Python to a bytearray is a common requirement, especially when dealing with low-level networking or file I/O operations where binary data is manipulated directly. For instance, you might have a list of integers [72, 101, 108, 108, 111]
which you want to convert to a bytearray equivalent, resulting in a sequence that can be further processed or transmitted as binary data.
Method 1: Using bytearray constructor
This method involves passing the list directly to the bytearray()
constructor, which converts the list of integers into a bytearray object. The function expects an iterable of integers in the range 0 <= x < 256, as each integer represents a byte.
Here’s an example:
my_list = [72, 101, 108, 108, 111] byte_array = bytearray(my_list)
Output:
bytearray(b'Hello')
In this snippet, the bytearray()
constructor takes a list my_list
and converts it into a bytearray comprising ASCII character equivalents of the integers provided, resulting in the word ‘Hello’.
Method 2: Using a for loop
Another approach is to create an empty bytearray and iterate over the list, appending each integer as a byte to the bytearray. This is useful when you need to filter or process the data while converting it.
Here’s an example:
my_list = [72, 101, 108, 108, 111] byte_array = bytearray() for i in my_list: byte_array.append(i)
Output:
bytearray(b'Hello')
This code snippet initializes an empty bytearray
and appends each element from the list my_list
one by one. The resultant bytearray is identical to the one produced by Method 1.
Method 3: Using list comprehension and bytes()
Combined list comprehension and the bytes()
function allows for a concise conversion, where the list comprehension creates a list of bytes, and the bytes()
function converts this list into an immutable bytes object. The bytearray()
then mutably extends the object.
Here’s an example:
my_list = [72, 101, 108, 108, 111] byte_array = bytearray([byte for byte in my_list])
Output:
bytearray(b'Hello')
The expression [byte for byte in my_list]
creates a new list consisting of the same elements as my_list
, and bytearray()
converts this list into a mutable bytearray.
Method 4: Using the map function
The map()
function can be used to map each integer in the list to its byte equivalent, and then the bytearray()
constructor is used to convert this map object into a bytearray.
Here’s an example:
my_list = [72, 101, 108, 108, 111] byte_array = bytearray(map(lambda x: x, my_list))
Output:
bytearray(b'Hello')
The lambda function within the map()
operation is redundant in this case as it simply returns each item unaltered, but it illustrates how you can apply transformations to the list elements if needed before conversion.
Bonus One-Liner Method 5: Using bytes with a generator expression
This elegant one-liner uses a generator expression within the bytes()
constructor to directly create an immutable bytes object from the list, which is then cast to a bytearray to make it mutable.
Here’s an example:
my_list = [72, 101, 108, 108, 111] byte_array = bytearray(bytes(my_list))
Output:
bytearray(b'Hello')
The generator expression bytes(my_list)
converts the list into a bytes object, which is then converted into a mutable bytearray.
Summary/Discussion
- Method 1: Direct Construction. Simple and efficient. No need for additional processing.
- Method 2: For loop. Versatile. Allows for element-wise processing but slightly more verbose.
- Method 3: List comprehension with bytearray. Compact. Makes read-only bytes mutable.
- Method 4: Map function. Functional programming style. Good for applying functions on elements, but can be overkill for simple copying.
- Method 5: Bytes with generator. Elegant one-liner. Converts list to bytes then to bytearray. Immutability of bytes object may be unnecessary intermediate step.