Python | Filter Tuple Dictionary Keys

Sometimes, while working with Python dictionaries, we can have it’s keys in form of tuples. A tuple can have many elements in it and sometimes, it can be essential to get them. If they are a part of a dictionary keys and we desire to get filtered tuple key elements, we need to perform certain functionalities to achieve this. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension
In this method, we just iterate through the each dictionary item and get it’s filtered key’s elements into a list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Filter Tuple Dictionary Keys
# Using list comprehension
  
# Initializing dict
test_dict = {(5, 6) : 'gfg', (1, 2, 8) : 'is', (9, 10) : 'best'}
  
# printing original dict
print("The original dict is : " + str(test_dict))
  
# Initializing K 
K = 5
  
# Filter Tuple Dictionary Keys
# Using list comprehension
res = [ele for key in test_dict for ele in key if ele > K]
  
# printing result
print("The filtered dictionary tuple key elements are : " + str(res))

chevron_right


Output :



The original dict is : {(5, 6): 'gfg', (9, 10): 'best', (1, 2, 8): 'is'}
The filtered dictionary tuple key elements are : [6, 9, 10, 8]

 

Method #2 : Using chain.from_iterable()
This task can be performed in more compact form, using one word instead of one-line by using from_iterable(), which internally accesses the tuple elements and stores in list and then perform the filter operation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Filter Tuple Dictionary Keys
# Using chain.from_iterable()
from itertools import chain
  
# Initializing dict
test_dict = {(5, 6) : 'gfg', (1, 2, 8) : 'is', (9, 10) : 'best'}
  
# printing original dict
print("The original dict is : " + str(test_dict))
  
# Initializing K 
K = 5
  
# Filter Tuple Dictionary Keys
# Using chain.from_iterable()
temp = list(chain.from_iterable(test_dict))
res = [ele for ele in temp if ele > K] 
  
# printing result
print("The filtered dictionary tuple key elements are : " + str(res))

chevron_right


Output :

The original dict is : {(5, 6): 'gfg', (9, 10): 'best', (1, 2, 8): 'is'}
The filtered dictionary tuple key elements are : [6, 9, 10, 8]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.