Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

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}

Is there a better way? I think there is a one-liner to do this. 

1 Answer

0 votes
by (36.8k points)

Just call dict(). on the list of tuples directly

>>> 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 

Browse Categories

...