# Python | Filter dictionary of tuples by condition

Sometimes, we can have a very specific problem in which we are given a tuple pair as values in dictionary and we need to filter dictionary items according to those pairs. This particular problem as use case in Many geometry algorithms in competitive programming. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using items() + dictionary comprehension These functionalities together can achieve this task. We can access all values using items() and condition can be imposed by dictionary comprehension.

## Python3

 `# Python3 code to demonstrate working of` `# Filter dictionary of tuples by condition` `# Using items() + dictionary comprehension`   `# initializing dictionary` `test_dict ``=` `{``'a'` `: (``6``, ``3``), ``'b'` `: (``4``, ``8``), ``'c'` `: (``8``, ``4``)}`   `# printing original dictionary` `print``("The original dictionary ``is` `: " ``+` `str``(test_dict))`   `# Filter dictionary of tuples by condition` `# Using items() + dictionary comprehension` `res ``=` `{key: val ``for` `key, val ``in` `test_dict.items() ``if` `val[``0``] >``=` `6` `and` `val[``1``] <``=` `4``}`   `# printing result` `print``("The filtered dictionary ``is` `: " ``+`  `str``(res))`

Time Complexity: O(n)

Space Complexity: O(n)

Output :

```The original dictionary is : {'b': (4, 8), 'a': (6, 3), 'c': (8, 4)}
The filtered dictionary is : {'a': (6, 3), 'c': (8, 4)}```

Method #2 : Using lambda + filter() This method works in similar way as above method with the exception of using the filter function instead of dictionary comprehension to compact logic. Works only with Python2.

## Python

 `# Python code to demonstrate working of` `# Filter dictionary of tuples by condition` `# Using lambda + filter()`   `# initializing dictionary` `test_dict ``=` `{``'a'` `: (``6``, ``3``), ``'b'` `: (``4``, ``8``), ``'c'` `: (``8``, ``4``)}`   `# printing original dictionary` `print``("The original dictionary ``is` `: " ``+` `str``(test_dict))`   `# Filter dictionary of tuples by condition` `# Using lambda + filter()` `res ``=` `dict``(``filter``(``lambda` `(x, (y, z)): y >``=` `6` `and` `z <``=` `4``, test_dict.items()))`   `# printing result` `print``("The filtered dictionary ``is` `: " ``+`  `str``(res))`

Time Complexity: O(n)

Space Complexity: O(n)

Output :

```The original dictionary is : {'b': (4, 8), 'a': (6, 3), 'c': (8, 4)}
The filtered dictionary is : {'a': (6, 3), 'c': (8, 4)}```

Method 3 : Using a for loop

step by step approach :

Create a dictionary called test_dict with 3 key-value pairs, where each value is a tuple containing 2 integers.
Print the original dictionary using the print() function and string concatenation.
Create an empty dictionary called new_dict.
Use a for loop to iterate through the items in test_dict.
For each item, check if the first value in the tuple is greater than or equal to 6 and the second value in the tuple is less than or equal to 4.
If the condition is true, add the item to new_dict.
Print the filtered dictionary using the print() function and string concatenation.
I hope that helps!

## Python3

 `# initializing dictionary` `test_dict ``=` `{``'a'``: (``6``, ``3``), ``'b'``: (``4``, ``8``), ``'c'``: (``8``, ``4``)}`   `# printing original dictionary` `print``(``"The original dictionary is: "` `+` `str``(test_dict))`   `# Filter dictionary of tuples by condition` `new_dict ``=` `{}` `for` `key, value ``in` `test_dict.items():` `    ``if` `value[``0``] >``=` `6` `and` `value[``1``] <``=` `4``:` `        ``new_dict[key] ``=` `value`   `# printing result` `print``(``"The filtered dictionary is: "` `+` `str``(new_dict))`

Output

```The original dictionary is: {'a': (6, 3), 'b': (4, 8), 'c': (8, 4)}
The filtered dictionary is: {'a': (6, 3), 'c': (8, 4)}```

The time complexity of this method is O(n), where n is the number of items in the dictionary.

The auxiliary space is also O(n), as we create a new dictionary to store the filtered items.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next