Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Get values of particular key in list of dictionaries

Improve Article
Save Article
  • Last Updated : 16 Jan, 2023
Improve Article
Save Article

Sometimes, we may require a way in which we have to get all the values of the specific key from a list of dictionaries. This kind of problem has a lot of applications in the web development domain in which we sometimes have a JSON and require just to get a 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 dictionaries for the desired value. 

Python3




# 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))


Output

The original list is : [{'gfg': 1, 'is': 2, '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




# 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))


Output

The original list is : [{'gfg': 1, 'is': 2, 'good': 3}, {'gfg': 2}, {'best': 3, 'gfg': 4}]
The values corresponding to key : [1, 2, 4]

Method #3 : Using Python Dictionary

Python3




# Python3 code to demonstrate working of
# Get values of particular key in list of dictionaries
 
res = {}
 
# 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))
 
for i in test_list:
    for j in i:
        if j in res.keys():
            res[j].append(i[j])
        else:
            res[j] = [i[j]]
 
# printing result
print("The values corresponding to key : " + str(res['gfg']))


Output

The original list is : [{'gfg': 1, 'is': 2, 'good': 3}, {'gfg': 2}, {'best': 3, 'gfg': 4}]
The values corresponding to key : [1, 2, 4]

Method #4: Using list comprehension and .get() method

Note: This approach also considers the case where the key does not exists.
Another approach to getting the values of a specific key in a list of dictionaries is to use list comprehension and the .get() method. The .get() method allows you to specify a default value to return if the key is not found in the dictionary. This can be useful for avoiding KeyError exceptions when working with a list of dictionaries that may not contain the same keys.

Python3




# Python3 code to demonstrate working of
# Get values of particular key in list of dictionaries
# Using list comprehension and .get() method
 
# 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 and .get() method
# Get values of particular key in list of dictionaries
res = [d.get('gfg', None) for d in test_list]
 
# printing result
print("The values corresponding to key : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : [{'gfg': 1, 'is': 2, 'good': 3}, {'gfg': 2}, {'best': 3, 'gfg': 4}]
The values corresponding to key : [1, 2, 4]

Time complexity: O(n) where n is the number of dictionaries in the list.
Auxiliary Space: O(n) where n is the number of values returned in the result list.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!