Open In App

Filter Dictionary Key based on the Values in Selective List – Python

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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’


Next Article

Similar Reads