Skip to content Skip to sidebar Skip to footer

How To Convert List To Dict

I have list ['a','b','c', 'd']. I want to concert it to dict where key is position every values in list and value is value of list. In output I should get something like this: { '0

Solution 1:

Use enumerate() to obtain the index of every element of the list and dict() to convert it to dictionary.

print(dict(enumerate(["a","b","c", "d"])))

Solution 2:

use dictionary comprehension.

>>> a = ["a","b","c", "d"]
>>> {i: j for i,j inenumerate(a)}
{0: 'a', 1: 'b', 2: 'c', 3: 'd'}

enumerate

Solution 3:

I think you should create a dict and then make it:

{'0' : list[0], '1' : list[1], '2' : list[2],'3' : list[3]} 

because I don't think you can convert a list to a dict in any other way

Post a Comment for "How To Convert List To Dict"