Create a dictionary with list comprehension in Python - GeeksforGeeks

02 Dec, 2021
In this article, we will discuss how to create a dictionary with list comprehension in Python.
Using dict() method we can convert list comprehension to the dictionary. Here we will pass the list_comprehension like a list of tuple values such that the first value act as a key in the dictionary and the second value act as the value in the dictionary.
Syntax: dict(list_comprehension)
Example 1: Python program to create a student list comprehension and convert it into a dictionary.
Python3
# create a list comprehension with student agedata = [('sravan', 23), ('ojaswi', 15),        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)] # using dict methoddict(data)
Output:
{'bobby': 20, 'gnanesh': 4, 'ojaswi': 15, 'rohith': 8, 'sravan': 23}
We can also use the following for loop inside the dict() method.
Syntax: dict([(key,value) for key,value in data])                         
Example 2:
Python3
# create a list comprehension with student agedata = [('sravan', 23), ('ojaswi', 15),        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)] # using dict method inside for loopdict([(key, value) for key, value in data])
Output:
{'bobby': 20, 'gnanesh': 4, 'ojaswi': 15, 'rohith': 8, 'sravan': 23}
Here in this method, we will create two lists such that values in the first list will be the keys and the second list values will be the values in the dictionary.
Syntax: dict(zip(key_list,value_list))
Example 1:
Python3
# create a list with student namename = ['sravan', 'ojaswi', 'rohith', 'gnanesh', 'bobby'] # create a list with student ageage = [23, 21, 32, 11, 23] # using dict method with zip()dict(zip(name, age))
Output:
{'bobby': 23, 'gnanesh': 11, 'ojaswi': 21, 'rohith': 32, 'sravan': 23}
Here we can iterate over the given date using iterable for loop.
Syntax: {key: value for (key, value) in data}
Example:
Python3
# create a list comprehension with student agedata = [('sravan', 23), ('ojaswi', 15),        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)]  # display using iterable method{key: value for (key, value) in data}
Output:
{'bobby': 20, 'gnanesh': 4, 'ojaswi': 15, 'rohith': 8, 'sravan': 23}
We can add a filter to the iterable to a list comprehension to create a dictionary only for particular data based on condition. Filtering means adding values to dictionary-based on conditions.
Syntax: {key: value for (key, value) in data condition}
Example:
Python3
# create a list comprehension with student agedata = [('sravan', 23), ('ojaswi', 15),        ('rohith', 8), ('gnanesh', 4), ('bobby', 20)]  # create a dictionary with list# comprehension if value is equal to 20print({key: value for (key, value) in data if value == 20}) # create a dictionary with list# comprehension if value is greater than  to 10print({key: value for (key, value) in data if value > 10}) # create a dictionary with list# comprehension if key is sravanprint({key: value for (key, value) in data if key == 'sravan'})
Output:
{'bobby': 20}
{'sravan': 23, 'ojaswi': 15, 'bobby': 20}
{'sravan': 23}
sagar0719kumar
sumitgumber28
Picked
Python dictionary-programs
python-dict
Python
Python Programs
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?