Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get back a list from bytes in Python? [duplicate]

Tags:

python

byte

I have a list in Python that I converted to bytes using bytes(). I want to get back that list and I am unsure how to do that. I searched around and it seemed to me like I can get back integers or strings not really the list itself. I am new to byte manipulation and maybe I am missing something obvious?

arr=[1,2,3,4,5]
arr2= bytes(arr)

I want to do something like arr1= *list*.from_bytes(arr2), which I am expecting to return arr1= [1,2,3,4,5].

like image 553
Shreya Sharma Avatar asked Oct 29 '22 03:10

Shreya Sharma


1 Answers

You can pass the bytes string into list() to convert back:

>>> lst = [1, 2, 3, 4, 5]
>>> bytes(lst)
b'\x01\x02\x03\x04\x05'
>>> list(bytes(lst))
[1, 2, 3, 4, 5]
like image 107
ggorlen Avatar answered Nov 27 '22 18:11

ggorlen