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
Following will be output for this program:
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'>