Convert List to Bytes & Vice Versa in Python (Examples)

 

Hi! This tutorial will show you how to turn a list to bytes object and vice-versa in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s jump into the Python code!

 

Create Sample List

Here, we will create the sample list that will be transformed into a bytes object. So, in your Python IDE, run the line of code below:

my_list = [0, 1, 2, 3, 4]

Above, we created a list of integers: my_list, for demonstration.

 

Example 1: List to Bytes | Transform List to Bytes Using bytes() Function

In this first example, we will turn a list into a bytes object using the built-in bytes() function.

my_bytes = bytes(my_list)
print(my_bytes)
 
# b'\x00\x01\x02\x03\x04'

You can check out the class of the object by running the following.

print(type(my_bytes))
 
# <class 'bytes'>

See, now you got bytes instead of a list.

 

Example 2: List to Bytes | Transform List to Bytes Using bytearray() & extend() Functions

In this second example, we will use Python’s built-in bytearray() function and extend() function to convert the list into a bytes object.

my_bytearray = bytearray()
my_bytearray.extend(my_list)
my_bytes = bytes(my_bytearray)
print(my_bytes)
 
# b'\x00\x01\x02\x03\x04'
 
 
print(type(my_bytes))
 
# <class 'bytes'>

The bytearray() function can be modified in place, meaning its contents can be changed by appending new values to individual bytes. That is why in the code above, we, first of all, created an empty bytes array, then extended it with the list; after which we converted it to bytes with the bytes() function.

Next, we will demonstrate how to convert a bytes object back into a list.

 

Example 1: Bytes to List | Transform Bytes to List Using list() Function

In this first example, we will use Python’s list() function to convert the bytes object back into a list.

new_list = list(my_bytes)
print(new_list)
 
# [0, 1, 2, 3, 4]
 
 
print(type(new_list))
 
# <class 'list'>

As seen, it is again a list, which is called new_list this time.

 

Example 2: Bytes to List | Transform Bytes to List Using map() Function

In this second example, we will use the map() function to transform the bytes object into a list.

new_list = list(map(int,my_bytes))
print(new_list)
 
# [0, 1, 2, 3, 4]
 
 
print(type(new_list))
 
# <class 'list'>

As shown, the map() function converts the items in the bytes object to integers. Then the list() function packs the items in a list.

 

Example 3: Bytes to List | Transform Bytes to List Using List Comprehension

In this third example, we will make use of list comprehension to turn the bytes object back into a list.

new_list = [x for x in my_bytes]
print(new_list)
 
# [0, 1, 2, 3, 4]
 
 
print(type(new_list))
 
# <class 'list'>

Thanks to the list comprehension method, the items in the bytes object were iterated inside a Python list construct to form a Python list from my_bytes.

With that, we have demonstrated how to turn a list into a bytes object and how to turn the bytes object back into a list in the Python programming language. I hope you found this tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to turn a list into a bytes object and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to turn a list into a bytes object and vice-versa in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to turn a list into a bytes object and vice-versa in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top