Skip to content
Related Articles

Related Articles

Python Program to extract Dictionaries with given Key from a list of dictionaries

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 24 Aug, 2022
Improve Article
Save Article

Given a list of dictionaries, the task is to write a python program that extracts only those dictionaries that contain a specific given key value.

Input : test_list = [{‘gfg’ : 2, ‘is’ : 8, ‘good’ : 3}, {‘gfg’ : 1, ‘for’ : 10, ‘geeks’ : 9}, {‘love’ : 3}], key= “gfg”
Output : [{‘gfg’: 2, ‘is’: 8, ‘good’: 3}, {‘gfg’ : 1, ‘for’ : 10, ‘geeks’ : 9}] 
Explanation : gfg is present in first two dictionaries, hence extracted.

Input : test_list = [{‘gfg’ : 2, ‘is’ : 8, ‘good’ : 3}, {‘gfg’ : 1, ‘for’ : 10, ‘geeks’ : 9}, {‘love’ : 3, ‘gfgs’ : 4}], key = “good”
Output : [{‘gfg’: 2, ‘is’: 8, ‘good’: 3}] 
Explanation : good is present in 1st dictionary, hence extracted. 

Method 1: Using list comprehension and keys()

In this, we test for the presence of a key using in operator, keys are extracted using the key(). List comprehension is used to iterate over different dictionaries.

Example:

Python3




# Python3 code to demonstrate working of
# Extract Dictionaries with given Key
# Using list comprehension + keys()
 
# initializing list
test_list = [{'gfg': 2, 'is': 8, 'good': 3},
             {'gfg': 1, 'for': 10, 'geeks': 9},
             {'love': 3}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key
key = 'gfg'
 
# checking for key using in operator
# keys() used to get keys
res = [sub for sub in test_list if key in list(sub.keys())]
 
# printing result
print("The filtered Dictionaries : " + str(res))

Output

The original list is : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}]

Time Complexity: O(n), where n is the length of the given list
Auxiliary Space: O(n)

Method 2 : Using filter() and lambda

In this, we perform the task of filtering using filter() and the lambda function is used to inject logic into filtration. The in operator is used to check the presence of a specific key.

Example:

Python3




# Python3 code to demonstrate working of
# Extract Dictionaries with given Key
# Using filter() + lambda
 
# initializing list
test_list = [{'gfg': 2, 'is': 8, 'good': 3},
             {'gfg': 1, 'for': 10, 'geeks': 9},
             {'love': 3, 'gfg': 4}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key
key = 'good'
 
# checking for key using in operator
# keys() used to get keys
# filter() + lambda used to perform filteration
res = list(filter(lambda sub: key in list(sub.keys()), test_list))
 
# printing result
print("The filtered Dictionaries : " + str(res))

Output

The original list is : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3, 'gfg': 4}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 3}]

Time Complexity: O(n), where n is the length of the given list
Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!