Open In App

Filter Dictionary of Tuples by Condition - Python

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

This task involves filtering the items of a dictionary based on a specific condition applied to the values, which are tuples in this case. We will check certain conditions for each tuple in the dictionary and select the key-value pairs that satisfy the condition.

Given the dictionary a = {'a': (6, 3), 'b': (4, 8), 'c': (8, 4)}, the goal is to filter the dictionary by a condition and then extract the keys into a tuple if the values meet the condition . For example, the condition could be that the first value of the tuple should be greater than or equal to 6 and the second value should be less than or equal to 4. Thus, the final output dictionary is {'a': (6, 3), 'c': (8, 4)}.

Using dictionary comprehension

Dictionary comprehension is a efficient method to filter a dictionary by condition. It combines iteration and condition checking into a single line, making it both faster and more readable.

Python
# initializing dictionary
a = {'a': (6, 3), 'b': (4, 8), 'c': (8, 4)}

d = {key: value for key, value in a.items() if value[0] >= 6 and value[1] <= 4}
print(str(d))

Output
{'a': (6, 3), 'c': (8, 4)}

Explanation:

  • Dictionary Comprehension: It iterates over each key-value pair in the dictionary a and the if condition checks if the first value of the tuple is greater than or equal to 6 and the second value is less than or equal to 4.

Using filter()

filter() function, when used with the dict() constructor, offers a functional approach to filter dictionary items. It allows for a clean way to rebuild a dictionary based on conditions, though it can be slightly less intuitive than dictionary comprehension.

Python
a = {'a': (6, 3), 'b': (4, 8), 'c': (8, 4)}

d = dict(filter(lambda item: item[1][0] >= 6 and item[1][1] <= 4, a.items()))
print(str(d))

Output
{'a': (6, 3), 'c': (8, 4)}

Explanation:

  • filter(): This filters the items of the dictionary based on the condition defined in the lambda function.
  • lambda() :This checks if the first value of the tuple is greater than or equal to 6 and second value of the tuple is less than or equal to 4..
  • a.items(): This provides the iterable of key-value pairs.

Using dict.update()

dict.update() method, when combined with a loop, is another way to filter a dictionary. It checks each key-value pair and adds the item to a new dictionary if the condition is met.

Python
a = {'a': (6, 3), 'b': (4, 8), 'c': (8, 4)}
# initialized empty dictionery
d = {}
for key, value in a.items():
    if value[0] >= 6 and value[1] <= 4:
        d.update({key: value})

print(str(d))

Output
{'a': (6, 3), 'c': (8, 4)}

Explanation:

  • for key, value in a.items():This loop iterates through each key-value pair in the dictionary a.
  • if value[0] >= 6 and value[1] <= 4:This condition checks if the first value of the tuple is greater than or equal to 6 and the second value is less than or equal to 4.
  • d.update({key: value}): If the condition is true, the update() method adds the current key-value pair to the dictionary d.

Practice Tags :

Similar Reads