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.
Python3
test_dict = {( 5 , 6 ) : 'gfg' , ( 1 , 2 , 8 ) : 'is' , ( 9 , 10 ) : 'best' }
print ("The original dict is : " + str (test_dict))
K = 5
res = [ele for key in test_dict for ele in key if ele > K]
print ("The filtered dictionary tuple key elements are : " + str (res))
|
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]
Time complexity: O(nm), where n is the number of keys in the dictionary and m is the maximum number of elements in a tuple key.
Auxiliary Space: O(m), where m is the maximum number of elements in a tuple key, for storing the filtered tuple elements in the result list.
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.
Python3
from itertools import chain
test_dict = {( 5 , 6 ) : 'gfg' , ( 1 , 2 , 8 ) : 'is' , ( 9 , 10 ) : 'best' }
print ("The original dict is : " + str (test_dict))
K = 5
temp = list (chain.from_iterable(test_dict))
res = [ele for ele in temp if ele > K]
print ("The filtered dictionary tuple key elements are : " + str (res))
|
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]
Time complexity: O(nm), where n is the number of keys in the dictionary and m is the maximum number of elements in a tuple key.
Auxiliary Space: O(m), where m is the maximum number of elements in a tuple key, for storing the filtered tuple elements in the result list.
Method #3 : Using keys(),extend(),list() methods
Approach
- Convert the keys of dictionary to single list using for loop + extend(),keys(),list() methods and store in x
- Extract elements greater than K and store in res
- Display res
Python3
test_dict = {( 5 , 6 ) : 'gfg' , ( 1 , 2 , 8 ) : 'is' , ( 9 , 10 ) : 'best' }
print ( "The original dict is : " + str (test_dict))
K = 5
x = []
for i in list (test_dict.keys()):
x.extend( list (i))
res = []
for i in x:
if (i>K):
res.append(i)
print ( "The filtered dictionary tuple key elements are : " + str (res))
|
Output
The original dict is : {(5, 6): 'gfg', (1, 2, 8): 'is', (9, 10): 'best'}
The filtered dictionary tuple key elements are : [6, 8, 9, 10]
Time complexity: O(nm), where n is the number of keys in the dictionary and m is the maximum number of elements in a tuple key.
Auxiliary Space: O(m), where m is the maximum number of elements in a tuple key, for storing the filtered tuple elements in the result list.
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...