Back
Here is how I am currently converting a list of tuples to the dictionary in Python:
l = [('a',1),('b',2)]h = {}[h.update({k:v}) for k,v in l]> [None, None]h> {'a': 1, 'b': 2}
l = [('a',1),('b',2)]
h = {}
[h.update({k:v}) for k,v in l]
> [None, None]
h
> {'a': 1, 'b': 2}
Is there a better way? I think there is a one-liner to do this.
Just call dict(). on the list of tuples directly
>>> my_list = [('a', 1), ('b', 2)]>>> dict(my_list){'a': 1, 'b': 2}
>>> my_list = [('a', 1), ('b', 2)]
>>> dict(my_list)
{'a': 1, 'b': 2}
If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch
31k questions
32.8k answers
501 comments
693 users