Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

Method 3 : Using for loop

Python3




# Initializing list of dictionaries
test_list = [{'gfg': 2, 'is': 8, 'good': 3},
             {'gfg': 1, 'for': 10, 'geeks': 9},
             {'love': 3}]
 
# Printing original list
print("The original list is: ", test_list)
 
# Initializing key
key = 'gfg'
 
# Using for loop to extract dictionaries with given key
result = []
for sub_dict in test_list:
    if key in sub_dict:
        result.append(sub_dict)
 
# Printing result
print("The filtered dictionaries: ", result)
#This code is contributed by Vinay Pinjala.


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)
Auxiliary Space: O(n)

Method 4: Using list comprehension and items()

Use items() method of dictionaries along with list comprehension to extract the dictionaries with the given key. In this method, iterate through the list of dictionaries using list comprehension, and check if the key exists in the dictionary using in operator with items() method.

Python3




# Python3 code to demonstrate working of
# Extract Dictionaries with given Key
# Using list comprehension + items()
 
# 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'
 
# extracting dictionaries with given key
res = [dct for dct in test_list if key in [k for k, v in dct.items()]]
 
# 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(NM), where N is the length of the list and M is the maximum number of key-value pairs in any dictionary of the list.
Auxiliary space: O(N), where N is the length of the list. This is because we are creating a new list to store the filtered dictionaries.

Method 5: Using dictionary comprehension and items()

Step-by-step approach:

  • Initialize the list of dictionaries test_list.
  • Print the original list using the print statement.
    Initialize the key key.
  • Use dictionary comprehension with items() method to create a list of dictionaries that contain the given key.
  • Print the resulting filtered list of dictionaries.

Python3




# Python3 code to demonstrate working of
# Extract Dictionaries with given Key
# Using dictionary comprehension + items()
 
# 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'
 
# using dictionary comprehension and items()
res = [d for d in test_list if key in d.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 input list of dictionaries, as we need to iterate over the entire list once to filter the dictionaries.
Auxiliary space: O(m), where m is the number of dictionaries that contain the given key, as we are creating a new list of those dictionaries.



Last Updated : 21 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads