Python Forum
Convert Bytearray into List using list()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert Bytearray into List using list()
Following is code for creating list from bytearray with proper indexing using list:
P.S.: Code can be small but I have inserted debug statements like print(), type(), etc. for understanding purpose

my_list = b'[1, 0, 1, 1]' 
my_list_conv = list(my_list)
print("Element at 0th index in my_list_conv = {}".format(my_list_conv[0])) 

print("Elements in my_list_conv:") 

for values in my_list_conv: 
        print(chr(values) , end=" ")

print("\nASCII converted element at 1st index: {}".format(chr(my_list_conv[1]))) 
print(type(my_list_conv))    ### Not a proper list i.e. indexing is not proper 

new_list = []    ### Created new_list for copying data for proper indexing 

for x in my_list_conv: 
        if (chr(x) == '1') or (chr(x) == '0'): 



Following will be output for this program:
Element at 0th index in my_list_conv = 91 Elements in my_list_conv: [ 1 , 0 , 1 , 1 ] ASCII converted element at 1st index: 1 <class 'list'> ['1', '0', '1', '1'] <class 'list'> 0 <class 'bytes'>
Is there a question?
What is the application for this? About the only time I run into byte arrays is serial ports or sockets. For those applications I would send an array of bytes as an array of bytes, not an array of ASCII codes for bytes. If I was sent an array of bytes encoded like this I would probably use decode() to convert the bytearray to a string, and then process the string. This converts your bytearray to an array of ints.
my_list = b'[1, 0, 1, 1]' 
my_bin_nums = [int(c) for c in my_list.decode() if c in '01']
print(my_bin_nums, my_bin_nums[1])
[1, 0, 1, 1] 0
Shlok likes this post

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why changing data in a copied list changes the original list? plumberpy 3 365 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Convert each element of a list to a string for processing tester_V 6 809 Jun-16-2021, 02:11 AM
Last Post: tester_V
  Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 913 Jun-10-2021, 08:35 PM
Last Post: perfringo
  convert numbers into list lokesh 1 569 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 1,833 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Order a list with successive permutations based on another list yvrob 3 751 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Saving list in a list quest_ 3 897 Mar-10-2021, 09:58 AM
Last Post: quest_
  convert List with dictionaries to a single dictionary iamaghost 3 805 Jan-22-2021, 03:56 PM
Last Post: iamaghost
Sad Convert python list to dictionary akanowhere 6 1,037 Dec-27-2020, 09:26 PM
Last Post: Pedroski55
  Adding List Element if Second part of the List Elements are the Same quest_ 3 751 Nov-25-2020, 04:33 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020