In this article, we will discuss the topic of how to convert a list to the dictionary in python. Before going to the actual topic let us take a quick brief of lists
and dictionaries
in python.
List: In python, a list is used to store the item of various data types like integer, float, string, etc. Python list is mutable i.e we can update the python list or simply say we can modify its element after its creation.
l=[1,"a","b",2,3,3.1] print(type(l))
Output
<class 'list'>
Dictionary: Dictionary is an unordered collection of data values that are used to store data in the form of key-value pairs.
d={"a":1, "b":2, "c":3, "d":4 } print(type(d))
Output
<class 'dict'>
Important Point:
A list is an ordered collection of data while a dictionary is an unordered collection of data so when we convert the list into dictionary output, may differ in order.
Ways to convert list to the dictionary in python
Method1-Using dictionary comprehension
This is one of the ways to convert a list to a dictionary in python. Let us understand what dictionary comprehension is. Dictionary comprehension is one of the easiest ways to create a dictionary. Using Dictionary comprehension we can make our dictionary by writing only a single line of code.
syntax: d = {key: value for variables in iterable}
Example :- d={ i: i*2 for i in range(1,6)}
d={i : i*2 for i in range(1,6)} print(d)
Output
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10}
Let us understand with an example how we can convert a list to a dictionary using this method. We clearly see in a dictionary comprehension syntax we iterate iterables and we know the list is also iterable so we simply iterate our list and form a dictionary with the list. Let us clear this concept with an example:-
l=[1,2,3,4,5] d={i : i*2 for i in l} print(d)
Output
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10}
Explanation: We simply pass a list in the dictionary comprehension and iterate through each element of the list. The element we get is store in i and we successfully executed the condition “i : i*2” to get the key-value pair.
Method-2 Using zip() function
The zip() function is used to aggregate or zip or combine the two values together. zip() function returns a zip object so we can easily convert this zip object to the tuple, list, or dictionary.
syntax:Â zip(iterator1,iterator2,…….)
As we convert the list to the dictionary so we need only two iterators and these are lists. That means we pass a list as an argument in the zip() function we get a zip object and then we can typecast this zip object to the dictionary. But here 2 cases will arise the first case when both lists have the same length second case when both lists have different lengths. Let us see both cases one by one.
First case: When both the list are of the same length
l1=['a','b','c','d'] l2=[1,2,3,4] zip_val=zip(l1,l2) print(zip_val) print(dict(zip_val))
Output
<zip object at 0x00000142497361C8> {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Explanation: Here we see that both the list are of the same length. We can see that the first zip() function returns a zip object that we print in the above example and then we typecast this object into the dictionary to get the desired result.
Second case: When both the list are of the different length
l1=['a','b','c','d','e'] l2=[1,2,3,4] zip_val=zip(l1,l2) print(zip_val) print(dict(zip_val))
Output
<zip object at 0x000001C331F160C8> {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Explanation: Here we see that our first list is of length 5 while the second list is of length 4 and in the final result, we get only 4 key-value pairs. So it can be concluded that the list with the smallest length will decide the number of key-value pairs in output. While another part is the same i.e. first we get a zip object and we typecast this zip object into the dictionary to get our desired result.
Method-3 Using dict() constructor
dict() constructor is also used to create a dictionary in python.This method comes into use when we have list of tuples. List of tuples means tuples inside the list.
Example: listofTuples = [(“student1” , 1), (“student2” , 2), (“Student3” , 3)] is a list of tuples.
listofTuples = [("student1" , 1), ("student2" , 2), ("Student3" , 3)] print(dict(listofTuples))
Output
{'student1': 1, 'student2': 2, 'Student3': 3}
so these are the three methods to convert a list to the dictionary in python.