Open In App

Python | Get all tuple keys from dictionary

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 all the 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 key’s elements into a list. 



Step-by-step approach:

Below is the implementation of the above approach:






# Python3 code to demonstrate working of
# Get all tuple keys from dictionary
# 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))
 
# Get all tuple keys from dictionary
# Using list comprehension
res = [ele for key in test_dict for ele in key]
 
# printing result
print("The dictionary tuple key elements are : " + str(res))

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

Time complexity: O(n), where n is the total number of elements in all the keys of the input dictionary. This is because the program iterates through all the keys of the dictionary and all the elements of each key, performing a constant-time operation on each element.
Auxiliary space: O(n), where n is the total number of elements in all the keys of the input dictionary. This is because the program creates a list to store the resulting key elements, which can have a maximum length of n. Therefore, the program uses O(n) additional memory to store its data structures.

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. 




# Python3 code to demonstrate working of
# Get all tuple keys from dictionary
# 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))
 
# Get all tuple keys from dictionary
# Using chain.from_iterable()
res = list(chain.from_iterable(test_dict))
 
# printing result
print("The dictionary tuple key elements are : " + str(res))

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

Time complexity: O(N), where N is the number of keys in the dictionary.
Auxiliary Space: O(N), where N is the number of keys in the dictionary. This is because we are creating a list of all the keys in the dictionary, which requires memory proportional to the number of keys.

 Method #3 : Using type(),items(),extend(),list() methods




# Python3 code to demonstrate working of
# Get all tuple keys from dictionary
 
# Initializing dict
test_dict = {(5, 6): 'gfg',  (9, 10): 'best', (1, 2, 8): 'is'}
 
# printing original dict
print("The original dict is : " + str(test_dict))
 
res = []
for key, value in test_dict.items():
    if type(key) is tuple:
        listTuple = list(key)
        res.extend(listTuple)
# printing result
print("The dictionary tuple key elements are : " + str(res))

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

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

Method 4:  Using the list() method. 

Get all tuple keys from a dictionary in Python is by using a loop to iterate through the keys of the dictionary and then flatten each tuple key into a list using the list() method. We can then add each element of the flattened list into a new list.




# 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))
 
# Get all tuple keys from dictionary using loop and flatten
res = []
for key in test_dict:
    res.extend(list(key))
 
# printing result
print("The dictionary tuple key elements are : " + str(res))

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

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

Method 5: Using a generator expression and the itertools.chain() method.




import itertools
 
# 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))
 
# Get all tuple keys from dictionary using generator expression and itertools.chain()
res = list(itertools.chain(*(key for key in test_dict)))
 
# printing result
print("The dictionary tuple key elements are : " + str(res))

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

Time complexity: O(n), where n is the total number of elements in all the tuple keys of the dictionary. This is because we iterate through each key in the dictionary exactly once and append its elements to the list.
Auxiliary space: O(n), since we create a list res to store all the tuple elements. Additionally, we also create a generator expression and an iterator object using the itertools.chain() method, but these do not take up any significant amount of memory. Therefore, the overall space complexity is also O(n).

Method 6:Using numpy:

 Step-by-step approach :




import numpy as np
from functools import reduce
 
# 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))
 
# Get all tuple keys from dictionary
# Using reduce() and numpy
res = np.array(reduce(lambda x, y: x + y, [list(key) for key in test_dict]))
 
# printing result
print("The dictionary tuple key elements are : " + str(res))
#This code is contributed by Jyothi pinjala

Output:

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

Time complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), where n is the length of the concatenated list of tuple elements.


Article Tags :