Open In App

Python – Filter dictionaries by values in Kth Key in list

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of dictionaries, the task is to write a Python program to filter dictionaries on basis of elements of Kth key in the list.

Examples:

Input : test_list = [{"Gfg" : 3, "is" : 5, "best" : 10},
                           {"Gfg" : 5, "is" : 1, "best" : 1},
                           {"Gfg" : 8, "is" : 3, "best" : 9},
                           {"Gfg" : 9, "is" : 9, "best" : 8},
                           {"Gfg" : 4, "is" : 10, "best" : 7}], K = "best", search_list = [1, 9, 8, 4, 5]

Output : [{'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}]

Explanation : Dictionaries with "best" as key and values other than 1, 9, 8, 4, 5 are omitted.
Input : test_list = [{"Gfg" : 3, "is" : 5, "best" : 10},
                           {"Gfg" : 5, "is" : 1, "best" : 1},
                           {"Gfg" : 8, "is" : 3, "best" : 9}], K = "best", search_list = [1, 9, 4, 5]

Output : [{'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}]

Explanation : Dictionaries with "best" as key and values other than 1, 9, 4, 5 are omitted.

Method #1 : Using loop + conditional statements 

In this, key-value pairs are added to the resultant dictionary after checking for Kth keys values in the list using conditionals, iterated using a loop.

Python3




# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using loop + conditional statements
 
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
             {"Gfg": 5, "is": 1, "best": 1},
             {"Gfg": 8, "is": 3, "best": 9},
             {"Gfg": 9, "is": 9, "best": 8},
             {"Gfg": 4, "is": 10, "best": 7}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing search_list
search_list = [1, 9, 8, 4, 5]
 
# initializing K
K = "best"
 
res = []
for sub in test_list:
 
    # checking if Kth key's value present in search_list
    if sub[K] in search_list:
        res.append(sub)
 
# printing result
print("Filtered dictionaries : " + str(res))


Output

The original list is : [{'Gfg': 3, 'is': 5, 'best': 10}, {'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}, {'Gfg': 4, 'is': 10, 'best': 7}]
Filtered dictionaries : [{'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2: Using list comprehension

Similar to the above method, list comprehension is used to provide shorthand to the method used above.

Python3




# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using list comprehension
 
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
             {"Gfg": 5, "is": 1, "best": 1},
             {"Gfg": 8, "is": 3, "best": 9},
             {"Gfg": 9, "is": 9, "best": 8},
             {"Gfg": 4, "is": 10, "best": 7}, ]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing search_list
search_list = [1, 9, 8, 4, 5]
 
# initializing K
K = "best"
 
# list comprehension as shorthand for solving problem
res = [sub for sub in test_list if sub[K] in search_list]
 
# printing result
print("Filtered dictionaries : " + str(res))


Output

The original list is : [{'Gfg': 3, 'is': 5, 'best': 10}, {'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}, {'Gfg': 4, 'is': 10, 'best': 7}]
Filtered dictionaries : [{'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3: Using filter() + lambda function

Python3




# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using filter() + lambda function
 
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
             {"Gfg": 5, "is": 1, "best": 1},
             {"Gfg": 8, "is": 3, "best": 9},
             {"Gfg": 9, "is": 9, "best": 8},
             {"Gfg": 4, "is": 10, "best": 7}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing search_list
search_list = [1, 9, 8, 4, 5]
 
# initializing K
K = "best"
 
# using filter() and lambda function to filter dictionaries
res = list(filter(lambda sub: sub[K] in search_list, test_list))
 
# printing result
print("Filtered dictionaries : " + str(res))


Output

The original list is : [{'Gfg': 3, 'is': 5, 'best': 10}, {'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}, {'Gfg': 4, 'is': 10, 'best': 7}]
Filtered dictionaries : [{'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using a for loop and a temporary list

Using a for loop and a temporary list to store the filtered dictionaries.

Python3




# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using for loop and temporary list
 
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
             {"Gfg": 5, "is": 1, "best": 1},
             {"Gfg": 8, "is": 3, "best": 9},
             {"Gfg": 9, "is": 9, "best": 8},
             {"Gfg": 4, "is": 10, "best": 7}]
 
# initializing search_list
search_list = [1, 9, 8, 4, 5]
 
# initializing K
K = "best"
 
# using for loop and temporary list to filter dictionaries
temp_list = []
for d in test_list:
    if d[K] in search_list:
        temp_list.append(d)
 
# storing filtered dictionaries in res
res = temp_list
 
# printing result
print("Filtered dictionaries : " + str(res))


Output

Filtered dictionaries : [{'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}]

Time complexity: O(nk), where n is the number of dictionaries in the list and k is the number of key-value pairs in each dictionary
Auxiliary space: O(nk), where n is the number of dictionaries in the list and k is the number of key-value pairs in each dictionary

Method #5: Using dictionary comprehension

  • Initializing list
  • printing original list
  • Initializing search_list
  • Initializing K
  • Using dictionary comprehension to filter dictionaries
  • printing result

Python3




# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using dictionary comprehension
 
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
             {"Gfg": 5, "is": 1, "best": 1},
             {"Gfg": 8, "is": 3, "best": 9},
             {"Gfg": 9, "is": 9, "best": 8},
             {"Gfg": 4, "is": 10, "best": 7}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing search_list
search_list = [1, 9, 8, 4, 5]
 
# initializing K
K = "best"
 
# using dictionary comprehension to filter dictionaries
res = [sub for sub in test_list if sub.get(K) in search_list]
 
# printing result
print("Filtered dictionaries : " + str(res))


Output

The original list is : [{'Gfg': 3, 'is': 5, 'best': 10}, {'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}, {'Gfg': 4, 'is': 10, 'best': 7}]
Filtered dictionaries : [{'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}]

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(k), where k is the number of filtered dictionaries (the length of the output list res).

Method #6: Using map() and lambda function

  • Define a lambda function that takes a dictionary d and returns d[K] (the value of the Kth key).
  • Use map() to apply the lambda function to each dictionary in test_list and store the result in a list.
  • Use filter() to filter the dictionaries in the list based on whether d[K] is in search_list.

Python3




# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using map() + lambda function + filter()
 
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
             {"Gfg": 5, "is": 1, "best": 1},
             {"Gfg": 8, "is": 3, "best": 9},
             {"Gfg": 9, "is": 9, "best": 8},
             {"Gfg": 4, "is": 10, "best": 7}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing search_list
search_list = [1, 9, 8, 4, 5]
 
# initializing K
K = "best"
 
# using map() + lambda function to extract Kth key value
values = list(map(lambda d: d[K], test_list))
 
# using filter() to filter dictionaries based on values
res = list(filter(lambda d: d[K] in search_list, test_list))
 
# printing result
print("Filtered dictionaries : " + str(res))


Output

The original list is : [{'Gfg': 3, 'is': 5, 'best': 10}, {'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}, {'Gfg': 4, 'is': 10, 'best': 7}]
Filtered dictionaries : [{'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}]

The time complexity of this approach is O(N), where N is the number of dictionaries in test_list, since both map() and filter() loop through the list once. The auxiliary space is O(N), since we create a list of length N to store the extracted values of the Kth key.

Method 7: Using list comprehension + any()

Initialize an empty list “res” to store the filtered dictionaries.
Use a list comprehension to iterate over “test_list” and check if the value of key “K” is present in “search_list” using the “any()” function.
If the condition is True, then append the current dictionary to the “res” list.
Print the filtered list of dictionaries.

Python3




# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using list comprehension and the any() function
 
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
             {"Gfg": 5, "is": 1, "best": 1},
             {"Gfg": 8, "is": 3, "best": 9},
             {"Gfg": 9, "is": 9, "best": 8},
             {"Gfg": 4, "is": 10, "best": 7}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing search_list
search_list = [1, 9, 8, 4, 5]
 
# initializing K
K = "best"
 
# using list comprehension and any() function to filter dictionaries based on values
res = [d for d in test_list if d[K] in search_list]
 
# printing result
print("Filtered dictionaries : " + str(res))


Output

The original list is : [{'Gfg': 3, 'is': 5, 'best': 10}, {'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}, {'Gfg': 4, 'is': 10, 'best': 7}]
Filtered dictionaries : [{'Gfg': 5, 'is': 1, 'best': 1}, {'Gfg': 8, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 9, 'best': 8}]

Time complexity: O(n), where “n” is the length of the input list “test_list”. This is because we are iterating over the list once using the list comprehension.
Auxiliary Space: O(k), where “k” is the length of the output list “res”. This is because we are storing the filtered dictionaries in a new list “res”.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads