
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a Dictionary with List Comprehension in Python
By using the dict() method in Python, we can create a dictionary with the list comprehension. Following is the syntax of dict() method-
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 a list comprehension.
Example
To iterate over 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 the string representation of Unicode integers, and the values are created by a loop over integers -
dict_= dict([(chr(i), i) for i in range(100, 105)]) print('Output dictionary: ', dict_) print(type(dict_))
Following is an output of the above code -
Output dictionary: {'d': 100, 'e': 101, 'f': 102, 'g': 103, 'h': 104} <class 'dict'>
Example
Here we have passed the two input lists, "data1" and "data2," to the list_comprehension using the 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))
Following is an output of the above code ?
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_))
Following is an output of the above code ?
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))
Following is an output of the above code ?
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'>