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
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 }]
print ( "The original list is : " + str (test_list))
search_list = [ 1 , 9 , 8 , 4 , 5 ]
K = "best"
res = []
for sub in test_list:
if sub[K] in search_list:
res.append(sub)
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
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 }, ]
print ( "The original list is : " + str (test_list))
search_list = [ 1 , 9 , 8 , 4 , 5 ]
K = "best"
res = [sub for sub in test_list if sub[K] in search_list]
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
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 }]
print ( "The original list is : " + str (test_list))
search_list = [ 1 , 9 , 8 , 4 , 5 ]
K = "best"
res = list ( filter ( lambda sub: sub[K] in search_list, test_list))
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
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 }]
search_list = [ 1 , 9 , 8 , 4 , 5 ]
K = "best"
temp_list = []
for d in test_list:
if d[K] in search_list:
temp_list.append(d)
res = temp_list
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
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 }]
print ( "The original list is : " + str (test_list))
search_list = [ 1 , 9 , 8 , 4 , 5 ]
K = "best"
res = [sub for sub in test_list if sub.get(K) in search_list]
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
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 }]
print ( "The original list is : " + str (test_list))
search_list = [ 1 , 9 , 8 , 4 , 5 ]
K = "best"
values = list ( map ( lambda d: d[K], test_list))
res = list ( filter ( lambda d: d[K] in search_list, test_list))
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
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 }]
print ( "The original list is : " + str (test_list))
search_list = [ 1 , 9 , 8 , 4 , 5 ]
K = "best"
res = [d for d in test_list if d[K] in search_list]
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”.
Similar Reads
Python Filter List of Dictionaries Based on Key Value
Python, a versatile and powerful programming language, offers multiple ways to manipulate and process data. When working with a list of dictionaries, you may often need to filter the data based on specific key-value pairs. In this article, we will explore three different methods to achieve this task
3 min read
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
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
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 - 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
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 | Sort dictionary by value list length
While working with Python, one might come to a problem in which one needs to perform a sort on dictionary list value length. This can be typically in case of scoring or any type of count algorithm. Let's discuss a method by which this task can be performed. Method 1: Using sorted() + join() + lambda
4 min read
Python | Unique dictionary filter in list
We are given a dictionary in list we need to find unique dictionary. For example, a = [ {"a": 1, "b": 2}, {"a": 1, "b": 2}, {"c": 3}, {"a": 1, "b": 3}] so that output should be [{'a': 1, 'b': 2}, {'a': 1, 'b': 3}, {'c': 3}]. Using set with frozensetUsing set with frozenset, we convert dictionary ite
3 min read