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()
#1
Star 
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'): 
                new_list.append(chr(x)) 



print(new_list) 
print(type(new_list)) 

print(new_list[1]) 
print(type(my_list))  


Following will be output for this program:
Output:
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'>
Reply
#2
Is there a question?
Reply
#3
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])
Output:
[1, 0, 1, 1] 0
Shlok likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert each element of a list to a string for processing tester_V 6 548 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 645 Jun-10-2021, 08:35 PM
Last Post: perfringo
  convert numbers into list lokesh 1 394 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 1,180 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Order a list with successive permutations based on another list yvrob 3 614 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Saving list in a list quest_ 3 741 Mar-10-2021, 09:58 AM
Last Post: quest_
  convert List with dictionaries to a single dictionary iamaghost 3 632 Jan-22-2021, 03:56 PM
Last Post: iamaghost
Sad Convert python list to dictionary akanowhere 6 842 Dec-27-2020, 09:26 PM
Last Post: Pedroski55
  Adding List Element if Second part of the List Elements are the Same quest_ 3 624 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Count number of occurrences of list items in list of tuples t4keheart 1 576 Nov-03-2020, 05:37 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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