Python | Get values of particular key in list of dictionaries
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:
- 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 | Combine the values of two dictionaries having same key
- Python | Sum list of dictionaries with same key
- Python | Set Difference in list of dictionaries
- Python | Flatten given list of dictionaries
- Python | Segregating key's value in list of dictionaries
- Python | Merging two list of dictionaries
- Python | Initialize list with empty dictionaries
- Python | Sort given list of dictionaries by date
- Python | Removing dictionary from list of dictionaries
- Python | Split dictionary of lists to list of dictionaries
- Python | Remove all values from a list present in other list
- Python | Sort the values of first list using second list
- Python | Filter even values from a 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.