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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputFiltered 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))
OutputThe 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))
OutputThe 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))
OutputThe 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".
Similar Reads
Filter List of Python Dictionaries by Key in Python
In Python, filtering a list of dictionaries based on a specific key is a common task when working with structured data. In this article, weâll explore different ways to filter a list of dictionaries by key from the most efficient to the least.Using List Comprehension List comprehension is a concise
3 min read
Python - Value limits to keys in Dictionaries List
Given a Dictionaries list, write a Python program to assign limits to each key in dictionary list.( each having all keys ). Examples: Input : test_list = [{"gfg" : 4, "is" : 7, "best" : 10}, {"gfg" : 2, "is" : 5, "best" : 9}, {"gfg" : 1, "is" : 2, "best" : 6}] Output : {'gfg': [1, 4], 'is': [2, 7],
11 min read
Python - Sort dictionaries list by Key's Value list index
Given list of dictionaries, sort dictionaries on basis of Key's index value. Input : [{"Gfg" : [6, 7, 8], "is" : 9, "best" : 10}, {"Gfg" : [2, 0, 3], "is" : 11, "best" : 19}, {"Gfg" : [4, 6, 9], "is" : 16, "best" : 1}], K = "Gfg", idx = 0 Output : [{'Gfg': [2, 0, 3], 'is': 11, 'best': 19}, {'Gfg': [
14 min read
Get First N Key:Value Pairs in given Dictionary - Python
We are given a dictionary and tasked with retrieving the first N key-value pairs. For example, if we have a dictionary like this: {'a': 1, 'b': 2, 'c': 3, 'd': 4} and we need the first 2 key-value pairs then the output will be: {'a': 1, 'b': 2}.Using itertools.islice()One of the most efficient ways
3 min read
Python - Test Kth index in Dictionary value list
Sometimes, while working with Python dictionaries, we can have a problem in which we need to test if, throughout the dictionary, the kth index of all values in the dictionary is equal to N. This kind of problem can occur in the web development domain. Let's discuss certain ways in which this problem
9 min read
Filter List Of Dictionaries in Python
Filtering a list of dictionaries is a fundamental programming task that involves selecting specific elements from a collection of dictionaries based on defined criteria. This process is commonly used for data manipulation and extraction, allowing developers to efficiently work with structured data b
2 min read
Filter Dictionary Key based on the Values in Selective List - Python
We are given a dictionary where each key is associated with a value and our task is to filter the dictionary based on a selective list of values. We want to retain only those key-value pairs where the value is present in the list. For example, we have the following dictionary and list: d = {'apple':
3 min read
Python - Sort Dictionary List by Key's ith Index value
Given List of dictionaries, sort dictionaries on basis of Key's ith index value Input : [{"Gfg" : "Best", "for" : "Geeks"}, {"Gfg" : "Good", "for" : "Me"}, {"Gfg" : "Better", "for" : "All"}], K = "Gfg", i = 1 Output : [{'Gfg': 'Best', 'for': 'Geeks'}, {'Gfg': 'Better', 'for': 'All'}, {'Gfg': 'Good',
7 min read
Python - Key with Maximum element at Kth index in Dictionary Value List
Given a dictionary with values as lists, the task is to write a Python program to get the key with the maximum element at the Kth index by comparing the elements of each list. Input : test_dict = {'Gfg' : [4, 6, 8, 2], 'is' : [1, 4, 5, 9], 'best' :[2, 3, 4, 10], 'for' :[4, 5, 2, 1], 'geeks' :[2, 10,
8 min read
Python - Add custom values key in List of dictionaries
The task of adding custom values as keys in a list of dictionaries involves inserting a new key-value pair into each dictionary within the list. In Python, dictionaries are mutable, meaning the key-value pairs can be modified or added easily. When working with a list of dictionaries, the goal is to
5 min read