Given a list of dictionaries, remove all dictionaries which don’t have K as a value.
Examples:
Input : test_list = [{"Gfg" : 4, "is" : 8, "best" : 9}, {"Gfg" : 3, "is": 7, "best" : 5}], K = 7 Output : [{'Gfg': 4, 'is': 8, 'best': 9}] Explanation : Resultant dictionary doesn't contain 7 as any element.
Input : test_list = [{"Gfg" : 4, "is" : 7, "best" : 9}, {"Gfg" : 3, "is": 7, "best" : 5}], K = 7 Output : [] Explanation : All contain 7 as element in List.
Method #1 : Using loop + values()
This is one of the ways in which this task can be performed. In this, we perform the task of iterating for all the dictionaries using a loop and check for value presence using values().
# Python3 code to demonstrate working of # Remove dictionary if K value not present # Using loop + values() # Initializing lists test_list = [{ "Gfg" : 4 , "is" : 8 , "best" : 9 },
{ "Gfg" : 5 , "is" : 8 , "best" : 1 },
{ "Gfg" : 3 , "is" : 7 , "best" : 6 },
{ "Gfg" : 3 , "is" : 7 , "best" : 5 }]
# Printing original list print ( "The original list : " + str (test_list))
# initializing K K = 7
res = []
# Checking for K element # using loop for sub in test_list:
if K not in list (sub.values()):
res.append(sub)
# Printing result print ( "Filtered dictionaries : " + str (res))
|
The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}] Filtered dictionaries : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}]
Method #2: Using list comprehension
This is yet another way in which this task can be performed. In this, we extract all the values using one-liner using list comprehension. The values are extracted using values().
# Python3 code to demonstrate working of # Remove dictionary if K value not present # Using list comprehension # initializing lists test_list = [{ "Gfg" : 4 , "is" : 8 , "best" : 9 },
{ "Gfg" : 5 , "is" : 8 , "best" : 1 },
{ "Gfg" : 3 , "is" : 7 , "best" : 6 },
{ "Gfg" : 3 , "is" : 7 , "best" : 5 }]
# printing original list print ( "The original list : " + str (test_list))
# initializing K K = 7
res = []
# using one-liner to extract dicts with NO K value # using not in operator to check presence res = [sub for sub in test_list if K not in list (sub.values())]
# printing result print ( "Filtered dictionaries : " + str (res))
|
The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}] Filtered dictionaries : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}]
Method #3: Using filter() and lambda function:
The function takes a list of dictionaries test_list and a value K as input. If the test_list is empty, the function returns an empty list. If the first dictionary in test_list contains the value K, the function returns a list containing that dictionary concatenated with the result of calling remove_dict_without_K with the rest of the list and the same value of K. Otherwise, the function simply returns the result of calling remove_dict_without_K with the rest of the list and the same value of K.
def remove_dict_without_K(test_list, K):
if not test_list:
return []
elif K in test_list[ 0 ].values():
return [test_list[ 0 ]] + remove_dict_without_K(test_list[ 1 :], K)
else :
return remove_dict_without_K(test_list[ 1 :], K)
# initializing lists test_list = [{ "Gfg" : 4 , "is" : 8 , "best" : 9 },
{ "Gfg" : 5 , "is" : 8 , "best" : 1 },
{ "Gfg" : 3 , "is" : 7 , "best" : 6 },
{ "Gfg" : 3 , "is" : 7 , "best" : 5 }]
# printing original list print ( "The original list : " + str (test_list))
# initializing K K = 7
res = list ( filter ( lambda x: K not in x.values(), test_list))
# printing result print ( "Filtered dictionaries : " + str (res))
|
The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}] Filtered dictionaries : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}]
Time complexity: O(nm), where n is the length of test_list and m is the average number of key-value pairs in each dictionary.
Auxiliary Space: O(nm), where n is the length of test_list and m is the average number of key-value pairs in each dictionary.
Method 4: Using dictionary comprehension method.
Steps:
- Initialize an empty dictionary to store the filtered dictionaries.
- Use a dictionary comprehension to iterate through each dictionary in the given list and filter out the dictionaries that have K as one of the values,
- Print the resulting dictionary of filtered dictionaries.
# Python3 code to demonstrate working of # Remove dictionary if K value not present # Using dictionary comprehension # Initializing lists test_list = [{ "Gfg" : 4 , "is" : 8 , "best" : 9 },
{ "Gfg" : 5 , "is" : 8 , "best" : 1 },
{ "Gfg" : 3 , "is" : 7 , "best" : 6 },
{ "Gfg" : 3 , "is" : 7 , "best" : 5 }]
# Printing original list print ( "The original list : " + str (test_list))
# Initializing K K = 7
# using dictionary comprehension to filter dictionaries filtered_dict = { str (i): dict_val for i, dict_val in enumerate (
test_list) if K not in dict_val.values()}
# Printing result print ( "Filtered dictionaries : " + str ( list (filtered_dict.values())))
|
The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}] Filtered dictionaries : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}]
Time complexity: O(n), where n is the number of dictionaries in the list.
Auxiliary space: O(n), as we are creating a new dictionary to store the filtered dictionaries.