How to Convert a List to Dictionary in Python?

By | June 11, 2021

List and Dictionaries are the two most prominent Python built-in data structures. A list is generally used to collect multiple elements of similar data types, and a dictionary is used to map the data (value) with the label (key).

In this Python tutorial, we will learn the different ways about How to convert a Python list to a Python dictionary.

Vamware

Let’s start with converting a list of tuple pairs into a dictionary

How to Convert a List of Tuples to Dictionary

The data structure of the Python Dictionary is very different from a Python list. A list stores its elements as a single unit and assigns an index number to every unit. But in the dictionary, an element is saved as a pair of key:value .

So when we want to convert a list into a dictionary we have to specify both the units, key, and value.

So in order to convert a list into a dictionary, we generally need a list of pair tuples. So when we convert the list into a dictionary the first element of the tuple becomes the key and the second value becomes the value for the key.

list = [(key1, value1), (key2, value2), (key3, value3)]

to dictionary we will get 
{key1:value1, key2:value2, key3:value3}

There are two ways to convert a list of tuples into a dictionary. We can either use the Python dict() function or use dictionary comprehension.

Let’s discuss both ways, first, we will convert a list of tuples into a dictionary using dict() function.

1. using dict() function 

The Python dict() function can accept a list of tuple pairs and convert it into a dictionary.

Example

# list of tuple pairs
my_list = [("Student1", "Rahul"), ("Student2", "Ravi"), ("Student3", "Aman"), ("Student4", "Vikas")]

# convert the list of tuple pairs into dictionary
my_dict = dict(my_list)

print(my_dict)

Output

{'Student1': 'Rahul', 'Student2': 'Ravi', 'Student3': 'Aman', 'Student4': 'Vikas'}

The dict() method can only convert a list of tuples or list pairs, into a dictionary. It needs two values at the same time so it can convert every element into key:value pair. If we apply the dict() function on a scalar list it will throw an error.

Example

 >>> my_list = [100, 200, 400, 500]
>>> dict(my_list)
Traceback (most recent call last):
TypeError: cannot convert dictionary update sequence element #0 to a sequence

 

2. Using dictionary comprehension 

Dictionary comprehension is a shorthand to create a dictionary using a for loop and an iterable within a single line expression.

If we have a list of tuple pairs we can convert it into a dictionary using dictionary comprehension.

Example

# list of tuple pairs
my_list = [("Student1", "Rahul"), ("Student2", "Ravi"), ("Student3", "Aman"), ("Student4", "Vikas")]

# convert the list of tuple pairs into dictionary
my_dict = {key:value for key, value in my_list}

print(my_dict)

Output

{'Student1': 'Rahul', 'Student2': 'Ravi', 'Student3': 'Aman', 'Student4': 'Vikas'}

How to Convert a Python List into Dictionary?

Let say we have a scalar list and we want to convert that list into a dictionary, where the list index becomes the key and the element becomes the value for the dictionary.

for instance

our list
[200, 300, 400, 500, 600, 700]
to dictionary
{0:200, 1:300, 2:400, 3:500, 4:600, 5:700}

To perform such conversion we can take the help of the Python zip function.

The zip function accepts two iterable objects and returns a zip iterator of tuples.

Example

>>> it1 = ["a", "b", "c", "d"]
>>> it2 = [1, 2, 3, 4]
>>> my_list = list(zip(it1, it2))
>>> my_list
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

The zip() function returns a zip iterator, so we use the list function to convert the zip iterator to a list of tuple pairs.

Now let’s use the zip function with dict() and dictionary comprehension and convert a scalar Python list to a dictionary.

Example: Using dict() function

# main list
values = ["A", "B", "C", "D", "E"]

# list of keys
keys = range(len(values))

# list of tuple (key,value) pairs
my_list = list(zip(keys, values))

# convert the list of tuple pairs into dictionary
my_dict = dict(my_list)

print(my_dict)

Output

{0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}

Example: Using dictionary comprehension

# main list
values = ["A", "B", "C", "D", "E"]

# list of keys
keys = range(len(values))

# list of tuple (key,value) pairs
my_list = list(zip(keys, values))

# convert the list of tuple pairs into dictionary
my_dict = {key:value for key, value in my_list}

print(my_dict)

Output

{0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}

Conclusion

In this Python tutorial, we discussed how to convert a Python list into a Python dictionary. To convert a list into a dictionary we can either use the dict() function or dictionary comprehension. The only thing we need to worry about that the list must be a collection of tuple pairs (key, value), so the newly created dictionary could have data for both keys and values.

Leave a Reply

Your email address will not be published. Required fields are marked *