Filter Dictionary Key based on the Values in Selective List - Python
Last Updated :
28 Jan, 2025
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': 10, 'banana': 20, 'cherry': 30, 'date': 40} and li= [10, 30] then the output will be {'apple': 10, 'cherry': 30}
Using Dictionary Comprehension
Using dictionary comprehension is one of the most efficient ways to filter a dictionary based on values from a list as this method allows us to iterate over the dictionary and check if each value exists in the selective list.
Python
d = {'apple': 10, 'banana': 20, 'cherry': 30, 'date': 40}
li = [10, 30]
res = {key: val for key, val in d.items() if val in li}
print(res)
Output{'apple': 10, 'cherry': 30}
Explanation:
- {key: val for key, val in d.items()} iterates through all key-value pairs in the dictionary d.
- if val in li : filters the key-value pairs, keeping only those where the value is present in li.
Using filter() with a Lambda Function
filter() function can also be used to filter dictionary items based on a condition and we can combine filter() with lambda function to check if the dictionary values are present in the selective_list. However filter() returns a filter object so we need to convert it back to a dictionary.
Python
d = {'apple': 10, 'banana': 20, 'cherry': 30, 'date': 40}
li = [10, 30]
res = dict(filter(lambda item: item[1] in li, d.items()))
print(res)
Output{'apple': 10, 'cherry': 30}
Explanation:
- d.items() returns an iterable of key-value pairs from the dictionary.
- filter(lambda item: item[1] in li, d.items()) filters the items based on whether the value (item[1]) is in li ( selective list ).
Using for Loop
This method involves manually iterating over the dictionary using a for loop, checking if the value is present in the selective_list and adding the key-value pair to a new dictionary if the condition is met.
Python
d = {'apple': 10, 'banana': 20, 'cherry': 30, 'date': 40}
li = [10, 30] # selective list
res = {}
for key, val in d.items():
if val in li:
res[key] = val
print(res)
Output{'apple': 10, 'cherry': 30}
Explanation:
- for key, val in d.items() iterates over each key-value pair in the dictionary d and if val in li: checks whether the value val is in the selective_list li.
- res[key] = val adds the key-value pair to the result dictionary res if the value is in the list.
Using NumPy
This method leverages the power of NumPy for efficient filtering. By converting the dictionary values to a NumPy array we can utilize vectorized operations to filter the dictionary based on the selective_list li.
Python
import numpy as np
d = {'apple': 10, 'banana': 20, 'cherry': 30, 'date': 40}
li = [10, 30] # selective list
# Convert dictionary values to a NumPy array and check which are in the selective list 'li'
values = np.array(list(d.values()))
mask = np.isin(values, li)
# Filter the dictionary based on the mask
res = {key: val for key, val in zip(d.keys(), values) if val in li}
print(res)
Output{'apple': np.int64(10), 'cherry': np.int64(30)}
Explanation:
- np.array(list(d.values())) converts the dictionary values into a NumPy array and np.isin(values, li) checks which of the values are in the 'li' thus returning a boolean array.
- zip(d.keys(), values) combines the keys and values into pairs and the dictionary comprehension is used to filter out those that are not in 'li'
Similar Reads
Python | Selective key values in dictionary Sometimes while working with Python dictionaries, we might have a problem in which we require to just get the selective key values from the dictionary. This problem can occur in web development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehensio
7 min read
Python - Filter dictionaries by values in Kth Key in list 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
9 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
Filtering a List of Dictionary on Multiple Values in Python Filtering a list of dictionaries is a common task in programming, especially when dealing with datasets. Often, you may need to extract specific elements that meet certain criteria. In this article, we'll explore four generally used methods for filtering a list of dictionaries based on multiple valu
4 min read
Remove Dictionary from List If Key is Equal to Value in Python Removing dictionaries from a list based on a specific condition is a common task in Python, especially when working with data in list-of-dictionaries format. In this article, we will see various methods to Remove Dictionary from List If the Key is Equal to the Value in Python.Using filter()filter()
2 min read