How to create a dictionary with list comprehension in Python?


By using the dict() method in python we can create a python dictionary with the list comprehension. The syntax of dict() method is given below. Following is the syntax for this

dict(**kwarg)

Keyword arguments. We can pass one or more keyword arguments. If no keyword argument is passed then the dict() method will create an empty dictionary object. The syntax for creating a dictionary with list comprehension:

dict(list_comprehension)

Creating dictionary using list comprehension

Instead of sending a number of keywords here we need to send a list of tuples with key value pairs to the dict() method. Let’s take an example and create a dictionary using the list comprehension.

Example

dict_= dict([(chr(i), i) for i in range(100, 105)])
print('Output dictionary: ', dict_)
print(type(dict_))

Output

Output dictionary:  {'d': 100, 'e': 101, 'f': 102, 'g': 103, 'h': 104}
<class 'dict'>

To iterate the for loop in list comprehension we used the range() method. And also we used another python built-in function chr() to get the string representation of the unicode integers. In the output dictionary the keys are created by string representation of unicode integers and the values are created by loop integers.

Example

Here we have passed the two input lists “data1” and “data2” to the list_comprehension using the python zip() method. This zip() method creates an iterator based on 2 inputs and finally the dictionary is created by using the list compression.

data1 = [1, 2, 3, 4, 5]
data2 = [10, 20, 30, 40, 50]
print('input list1: ', data1)
print('input list12: ', data2)

# create a dict using list comprehension
d = dict([(key, value) for key, value in zip(data1,data2)])
print('Output dictionary: ', d)
print(type(d))

Output

input list1:  [1, 2, 3, 4, 5]
input list12:  [10, 20, 30, 40, 50]
Output dictionary:  {1: 10, 2: 20, 3: 30, 4: 40, 5: 50}
<class 'dict'>

Example

In the following example using the python list comprehension technique, we have created a list of tuples, and each tuple has 2 elements. These two elements are then converted as keys and values to the dictionary object.

l = [( i,i*2) for i in range(1,10)]
print("Comprehension output:",l)

dict_= dict(l)
print('Output dictionary: ', dict_)
print(type(dict_))

Output

Comprehension output: [(1, 2), (2, 4), (3, 6), (4, 8), (5, 10), (6, 12), (7, 14), (8, 16), (9, 18)]
Output dictionary:  {1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
<class 'dict'>

Example

Finally let's take another example and see how to create a dictionary with list comprehension in python.

l = [20, 21, 65, 29, 76, 98, 35]
print('Input list: ', l)

# create a dict using list comprehension
d = dict([(val/2, val) for val in l])
print('Output dictionary: ', d)
print(type(d))

Output

Input list:  [20, 21, 65, 29, 76, 98, 35]
Output dictionary:  {10.0: 20, 10.5: 21, 32.5: 65, 14.5: 29, 38.0: 76, 49.0: 98, 17.5: 35}
<class 'dict'>

By iterating list elements using list comprehension we have successfully created a dictionary.

Updated on: 24-Aug-2023

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements