Given a List of dictionaries, extract all the dictionaries which have empty strings as values of a particular key.
Input : test_list = [{"Gfg" : "4", "is" : "good", "best" : "1"},
{"Gfg" : "9", "is" : "CS", "best" : "10"}], K = "Gfg"
Output : []
Explanation : No "Gfg" key is empty.
Input : test_list = [{"Gfg" : "", "is" : "good", "best" : "1"},
{"Gfg" : "9", "is" : "CS", "best" : "10"}], K = "Gfg"
Output : [{"Gfg" : "", "is" : "good", "best" : "1"}]
Explanation : Dictionary with empty "Gfg" extracted.
Method #1: Using list comprehension
This is one of the ways in which this task can be performed. In this, we run a loop through all dictionaries and check for key’s empty string value. All this was compiled in list comprehension rather than loop.
Step-by-step approach :
- Initializes a list of dictionaries called test_list.
- Prints the original list using the print() function.
- Initializes a variable called K with the string value “Gfg”.
- Use list comprehension to iterate over each dictionary in test_list and filters the dictionaries with an empty string value for the key K.
- The filtered dictionaries are stored in a new list called res.
- Prints the extracted dictionaries using the print() function.
Python3
test_list = [{ "Gfg" : "4" , "is" : "good" , "best" : "1" },
{ "Gfg" : " ", " is " : " better ", " best " : " 8 "},
{ "Gfg" : "9" , "is" : "CS" , "best" : "10" }]
print ( "The original list : " + str (test_list))
K = "Gfg"
res = [sub for sub in test_list if sub[K] = = '']
print ( "The extracted dictionaries : " + str (res))
|
Output
The original list : [{'Gfg': '4', 'is': 'good', 'best': '1'}, {'Gfg': '', 'is': 'better', 'best': '8'}, {'Gfg': '9', 'is': 'CS', 'best': '10'}]
The extracted dictionaries : [{'Gfg': '', 'is': 'better', 'best': '8'}]
Time Complexity: O(n), where n is the number of dictionaries in the list.
Auxiliary Space: O(n), where n is the number of dictionaries in the list.
Method #2 : Using filter() + lambda
This is yet another way in which this task can be performed. In this, we extract all the empty values key’s dictionaries using filter() and functionality and iteration by lambda.
Python3
test_list = [{ "Gfg" : "4" , "is" : "good" , "best" : "1" },
{ "Gfg" : " ", " is " : " better ", " best " : " 8 "},
{ "Gfg" : "9" , "is" : "CS" , "best" : "10" }]
print ( "The original list : " + str (test_list))
K = "Gfg"
res = list ( filter ( lambda sub: sub[K] = = '', test_list))
print ( "The extracted dictionaries : " + str (res))
|
Output
The original list : [{'Gfg': '4', 'is': 'good', 'best': '1'}, {'Gfg': '', 'is': 'better', 'best': '8'}, {'Gfg': '9', 'is': 'CS', 'best': '10'}]
The extracted dictionaries : [{'Gfg': '', 'is': 'better', 'best': '8'}]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using len() and keys() methods
Python3
test_list = [{ "Gfg" : "4" , "is" : "good" , "best" : "1" },
{ "Gfg" : " ", " is " : " better ", " best " : " 8 "},
{ "Gfg" : "9" , "is" : "CS" , "best" : "10" }]
print ( "The original list : " + str (test_list))
K = "Gfg"
res = []
for i in test_list:
if K in i.keys() and len (i[K]) = = 0 :
res.append(i)
print ( "The extracted dictionaries : " + str (res))
|
Output
The original list : [{'Gfg': '4', 'is': 'good', 'best': '1'}, {'Gfg': '', 'is': 'better', 'best': '8'}, {'Gfg': '9', 'is': 'CS', 'best': '10'}]
The extracted dictionaries : [{'Gfg': '', 'is': 'better', 'best': '8'}]
Time complexity of this code is O(n), where n is the length of the input list test_list, because we are iterating over each dictionary in the list once.
Auxiliary space used by this code is also O(n), because we are creating a list res to store the dictionaries that have an empty string as value for the key K.
Method 4: Using for loop and if statement
The loop iterates through each dictionary in test_list, and the if statement checks if the value associated with the key K is an empty string. If it is, the dictionary is appended to the res list. Finally, the res list is printed.
Python3
test_list = [{ "Gfg" : "4" , "is" : "good" , "best" : "1" },
{ "Gfg" : " ", " is " : " better ", " best " : " 8 "},
{ "Gfg" : "9" , "is" : "CS" , "best" : "10" }]
print ( "The original list : " + str (test_list))
K = "Gfg"
res = []
for sub in test_list:
if sub[K] = = '':
res.append(sub)
print ( "The extracted dictionaries : " + str (res))
|
Output
The original list : [{'Gfg': '4', 'is': 'good', 'best': '1'}, {'Gfg': '', 'is': 'better', 'best': '8'}, {'Gfg': '9', 'is': 'CS', 'best': '10'}]
The extracted dictionaries : [{'Gfg': '', 'is': 'better', 'best': '8'}]
Time Complexity: O(n), where n is the number of dictionaries in test_list. This is because the loop iterates through each dictionary in test_list exactly once.
Auxiliary Space: O(k), where k is the number of dictionaries in test_list that have an empty string value associated with the key K. This is because the res list will have at most k dictionaries appended to it.
Method 5: Using map() + lambda
In this approach, the lambda function inside the map() function returns the dictionary if the value of the K key is an empty string, and None otherwise. The filter() function then removes all the None values from the resulting list.
Python3
test_list = [{ "Gfg" : "4" , "is" : "good" , "best" : "1" },
{ "Gfg" : " ", " is " : " better ", " best " : " 8 "},
{ "Gfg" : "9" , "is" : "CS" , "best" : "10" }]
K = "Gfg"
res = list ( filter ( lambda x: x ! = None , map ( lambda x: x if x[K] = = '' else None , test_list)))
print ( "The extracted dictionaries : " + str (res))
|
Output
The extracted dictionaries : [{'Gfg': '', 'is': 'better', 'best': '8'}]
Time Complexity: O(n), where n is the number of dictionaries in the input list.
Auxiliary Space: O(k), where k is the number of dictionaries in the output list.
Method 6: Using the pandas library
- Import the pandas library using the import keyword and the pandas module.
- Initialize a list of dictionaries called test_list. Each dictionary represents a row of data, with keys representing column names and values representing the data in each column.
- Print the original list of dictionaries using the print() function and the str() function to convert the list to a string.
- Initialize a variable K to the key that you want to search for empty string values in.
- Create a pandas dataframe from the list of dictionaries using the pd.DataFrame() method. This will create a table with columns corresponding to the keys of the dictionaries and rows corresponding to the dictionaries in the list.
- Select the dictionaries with an empty string value in the K key using boolean indexing. This can be done by creating a boolean mask with the condition df[K] == ” and passing it as an index to the dataframe.
- Convert the resulting dataframe to a list of dictionaries using the to_dict() method with the argument ‘records’. This will create a list of dictionaries where each dictionary represents a row of data.
- Store the resulting list of dictionaries in a variable called res.
- Print the extracted dictionaries using the print() function and the str() function to convert the list to a string.
Python3
import pandas as pd
test_list = [{ "Gfg" : "4" , "is" : "good" , "best" : "1" },
{ "Gfg" : " ", " is " : " better ", " best " : " 8 "},
{ "Gfg" : "9" , "is" : "CS" , "best" : "10" }]
print ( "The original list : " + str (test_list))
K = "Gfg"
df = pd.DataFrame(test_list)
res = df[df[K] = = ' '].to_dict(' records')
print ( "The extracted dictionaries : " + str (res))
|
OUTPUT:
The original list : [{'Gfg': '4', 'is': 'good', 'best': '1'}, {'Gfg': '', 'is': 'better', 'best': '8'}, {'Gfg': '9', 'is': 'CS', 'best': '10'}]
The extracted dictionaries : [{'Gfg': '', 'is': 'better', 'best': '8'}]
Time complexity: O(n), where n is the number of dictionaries in the list.
Auxiliary space: O(n), where n is the number of dictionaries in the list (due to the creation of the pandas dataframe)
Last Updated :
04 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...