Sometimes, we may require a way in which we have to get all the values of specific key from a list of dictionary. This kind of problem has a lot of application in web development domain in which we sometimes have a json and require just to get single column from records. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension
Using list comprehension is quite straight forward method to perform this particular task. In this, we just iterate over the list of dictionary for desired value.
# Python3 code to demonstrate working of # Get values of particular key in list of dictionaries # Using list comprehension # initializing list test_list = [{'gfg' : 1, 'is' : 2, 'good' : 3}, {'gfg' : 2}, {'best' : 3, 'gfg' : 4}] # printing original list print("The original list is : " + str(test_list)) # Using list comprehension # Get values of particular key in list of dictionaries res = [ sub['gfg'] for sub in test_list ] # printing result print("The values corresponding to key : " + str(res)) |
The original list is : [{‘is’: 2, ‘gfg’: 1, ‘good’: 3}, {‘gfg’: 2}, {‘best’: 3, ‘gfg’: 4}]
The values corresponding to key : [1, 2, 4]
Method #2 : Using map() + itemgetter()
This problem can also be solved using another technique using map() and itemgetter(). In this, map is used to link the value to all the dictionary keys and itemgetter gets the desired key.
# Python3 code to demonstrate working of # Get values of particular key in list of dictionaries # Using map() + itemgetter() from operator import itemgetter # initializing list test_list = [{'gfg' : 1, 'is' : 2, 'good' : 3}, {'gfg' : 2}, {'best' : 3, 'gfg' : 4}] # printing original list print("The original list is : " + str(test_list)) # Using map() + itemgetter() # Get values of particular key in list of dictionaries res = list(map(itemgetter('gfg'), test_list)) # printing result print("The values corresponding to key : " + str(res)) |
The original list is : [{‘is’: 2, ‘gfg’: 1, ‘good’: 3}, {‘gfg’: 2}, {‘best’: 3, ‘gfg’: 4}]
The values corresponding to key : [1, 2, 4]
Recommended Posts:
- Python - Add custom values key in List of dictionaries
- Python - List of dictionaries all values Summation
- Python - Group single item dictionaries into List values
- Ways to sort list of dictionaries by values in Python – Using itemgetter
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Python - Convert Dictionaries List to Order Key Nested dictionaries
- Python | Combine the values of two dictionaries having same key
- Python - Values frequency across Dictionaries lists
- Python - Convert List of Dictionaries to List of Lists
- Python - Convert list of dictionaries to Dictionary Value list
- Python - Sort dictionaries list by Key's Value list index
- Python - Convert List to List of dictionaries
- Python | Sum list of dictionaries with same key
- Python | Segregating key's value in list of dictionaries
- Count dictionaries in a list in Python
- Python | Set Difference in list of dictionaries
- Python | Flatten given list of dictionaries
- Python | Merging two list of dictionaries
- Python - Convert String to List of dictionaries
- Python - Extract Key's value from Mixed Dictionaries List
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

