I have list:
my_list=[['a',1], ['b', 2]]
I want my_dict['a']=1 and my_dict['b']=2
Is there easy way, without looping?
I have list:
my_list=[['a',1], ['b', 2]]
I want my_dict['a']=1 and my_dict['b']=2
Is there easy way, without looping?
There is, like this:
>>> my_list=[['a',1], ['b', 2]]
>>> my_dict = dict(my_list)
>>> my_dict
{'a': 1, 'b': 2}
Basically your list is already compatible with the dict
constructor. (i.e: A list of 2-item tuples/lists or more precisely an iterable that yields 2-item lists/tuples -- key/value pairs).