I have a list with the following value
[(b'abc', '123'), (b'xyz', '456'), (b'cde', '785')]
I need to change it to map with key value pair :
('abc','123'),('xyz','456'),('cde','785')
Is there a method that I can use.
I have a list with the following value
[(b'abc', '123'), (b'xyz', '456'), (b'cde', '785')]
I need to change it to map with key value pair :
('abc','123'),('xyz','456'),('cde','785')
Is there a method that I can use.
In python, dictionaries are the simplest implementation of a map.
L=[(b'abc', '123'), (b'xyz', '456'), (b'cde', '785')]
d={}
for i in L:
d[i[0]]=i[1]
print(d)
This code will turn your list into a dictionary
Hope it helps✌️
my_list = [(b'abc', '123'), (b'xyz', '456'), (b'cde', '785')]
dic={}
for key,value in my_list:
dic[key.decode()]=value
print(dic) #{'abc': '123', 'xyz': '456', 'cde': '785'}
You can utilise the dict() function in conjunction with a generator as follows:
L = [(b'abc', '123'), (b'xyz', '456'), (b'cde', '785')]
D = dict((x.decode(), y) for x, y in L)
print(D)
Output:
{'abc': '123', 'xyz': '456', 'cde': '785'}