Python Program to extract Dictionaries with given Key from a list of dictionaries
Last Updated :
21 Apr, 2023
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
test_list = [{ 'gfg' : 2 , 'is' : 8 , 'good' : 3 },
{ 'gfg' : 1 , 'for' : 10 , 'geeks' : 9 },
{ 'love' : 3 }]
print ( "The original list is : " + str (test_list))
key = 'gfg'
res = [sub for sub in test_list if key in list (sub.keys())]
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
test_list = [{ 'gfg' : 2 , 'is' : 8 , 'good' : 3 },
{ 'gfg' : 1 , 'for' : 10 , 'geeks' : 9 },
{ 'love' : 3 , 'gfg' : 4 }]
print ( "The original list is : " + str (test_list))
key = 'good'
res = list ( filter ( lambda sub: key in list (sub.keys()), test_list))
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
test_list = [{ 'gfg' : 2 , 'is' : 8 , 'good' : 3 },
{ 'gfg' : 1 , 'for' : 10 , 'geeks' : 9 },
{ 'love' : 3 }]
print ( "The original list is: " , test_list)
key = 'gfg'
result = []
for sub_dict in test_list:
if key in sub_dict:
result.append(sub_dict)
print ( "The filtered dictionaries: " , result)
|
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
test_list = [{ 'gfg' : 2 , 'is' : 8 , 'good' : 3 },
{ 'gfg' : 1 , 'for' : 10 , 'geeks' : 9 },
{ 'love' : 3 }]
print ( "The original list is : " + str (test_list))
key = 'gfg'
res = [dct for dct in test_list if key in [k for k, v in dct.items()]]
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
test_list = [{ 'gfg' : 2 , 'is' : 8 , 'good' : 3 },
{ 'gfg' : 1 , 'for' : 10 , 'geeks' : 9 },
{ 'love' : 3 }]
print ( "The original list is : " + str (test_list))
key = 'gfg'
res = [d for d in test_list if key in d.keys()]
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.
Please Login to comment...