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
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  Value error when converting hex value to bytearray shubhamjainj 7 10,331 Mar-20-2023, 05:30 PM
Last Post: Skaperen
  appending to a bytearray Skaperen 21 13,342 Mar-19-2023, 11:05 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,491 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  convert string to float in list jacklee26 6 1,815 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Save multiple Parts of Bytearray to File ? lastyle 1 908 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  convert a list to links Pir8Radio 3 1,043 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  bytearray object - why converting to ascii? Chepilo 2 1,531 Nov-21-2022, 07:25 PM
Last Post: Chepilo

Forum Jump:

User Panel Messages

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