Like List Comprehension, Python allows dictionary comprehensions. We can create dictionaries using simple expressions.
A dictionary comprehension takes the form {key: value for (key, value) in iterable}
Let’s see a example,lets assume we have two lists named keys and value now,
# Python code to demonstrate dictionary # comprehension # Lists to represent keys and values keys = ['a','b','c','d','e'] values = [1,2,3,4,5] # but this line shows dict comprehension here myDict = { k:v for (k,v) in zip(keys, values)} # We can use below too # myDict = dict(zip(keys, values)) print (myDict) |
Output :
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
We can also make dictionary from a list using comprehension
# Python code to demonstrate dictionary # creation using list comprehension myDict = {x: x**2 for x in [1,2,3,4,5]} print (myDict) |
Output :
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
and like,
sDict = {x.upper(): x*3 for x in 'coding '} print (sDict) |
Output :
{'O': 'ooo', 'N': 'nnn', 'I': 'iii', 'C': 'ccc', 'D': 'ddd', 'G': 'ggg'}
We can use Dictionary comprehensions with if and else statements and with other expressions too. This example below maps the numbers to their cubes that are not divisible by 4:
# Python code to demonstrate dictionary # comprehension using if. newdict = {x: x**3 for x in range(10) if x**3 % 4 == 0} print(newdict) |
Output :
{0: 0, 8: 512, 2: 8, 4: 64, 6: 216}
This article is contributed by Shantanu Sharma.. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python program to update a dictionary with the values from a dictionary list
- Python List Comprehension and Slicing
- Move all zeroes to end of array using List Comprehension in Python
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- List comprehension and ord() in Python to remove all characters other than alphabets
- Count set bits using Python List comprehension
- Python List Comprehension to find pair with given sum from two arrays
- Python List Comprehension | Segregate 0's and 1's in an array list

