Python | Unpacking dictionary keys into tuple
Last Updated :
14 Aug, 2023
In certain cases, we might come into a problem in which we require to unpack dictionary keys to tuples. This kind of problem can occur in cases we are just concerned about the keys of dictionaries and wish to have a tuple of them. Let’s discuss certain ways in which this task can be performed in Python.
Example
Input: {'Welcome': 1, 'to': 2, 'GFG': 3}
Output: ('Welcome', 'to', 'GFG')
Explanation: In this, we are unpacking the keys of Dictionary into Tuple in Python.
Unpacking Dictionary keys into Tuple using Tuple()
The simple type casting of a dictionary into a tuple in fact does the required task. This function takes just the keys and converts them into key tuples as required.
Python3
test_dict = { 'Gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ( "The original dictionary is : " + str (test_dict))
res = tuple (test_dict.keys())
print ( "The unpacked dict. keys into tuple is : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'is': 2, 'best': 3}
The unpacked dict. keys into tuple is : ('Gfg', 'is', 'best')
Time complexity: O(1). This is because the program performs a constant number of operations to create a tuple from the keys of the dictionary. The time required to create the tuple is independent of the size of the input dictionary.
Auxiliary space: O(k), where k is the number of keys in the input dictionary.
Unpacking Dictionary keys into Tuple using the “=” operator and multiple variables
The ‘=’ operator method can also be used to perform this particular task. In this, we assign the comma-separated variables to the dictionary. We use as many variables as keys in the dictionary. This method is not recommended in case of unknown or many keys.
Python3
test_dict = { 'Gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ( "The original dictionary is:" , test_dict)
keys_tuple = tuple (key for key in test_dict)
print ( "Unpacked dictionary keys into tuple:" , keys_tuple)
|
Output
The original dictionary is: {'Gfg': 1, 'is': 2, 'best': 3}
Unpacked dictionary keys into tuple: ('Gfg', 'is', 'best')
Time Complexity: O(1)
Auxiliary Space: O(1)
Unpacking Dictionary keys into Tuple use keys() Method + Tuple()
Unpack the keys of a dictionary into a tuple is to use the keys() method of the dictionary to get a list of keys, and then pass that list to the tuple() constructor
Python3
test_dict = { 'Gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ( "The original dictionary is: " + str (test_dict))
res = tuple (test_dict.keys())
print ( "The unpacked dict. keys into tuple is: " + str (res))
|
Output
The original dictionary is: {'Gfg': 1, 'is': 2, 'best': 3}
The unpacked dict. keys into tuple is: ('Gfg', 'is', 'best')
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.
Unpacking Dictionary keys into Tuple use the items() Method
We are using the items() method of the dictionary and iterating over it to create the tuple of keys in Python. The dictionary method generates a view object that showcases a list of key-value pairs from the dictionary as tuples. Each tuple comprises a key and its corresponding value. This technique offers an efficient approach to obtaining access to both keys and values at once, making it a useful tool for various data manipulation tasks.
Python3
test_dict = { 'Gfg' : 1 , 'is' : 2 , 'best' : 3 }
items = test_dict.items()
keys = [key for key, value in items]
res = tuple (keys)
print ( "The original dictionary is : " + str (test_dict))
print ( "The unpacked dict. keys into tuple is : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'is': 2, 'best': 3}
The unpacked dict. keys into tuple is : ('Gfg', 'is', 'best')
Time complexity: O(n) since we need to iterate over all key-value pairs in the dictionary.
Auxiliary space: O(n) as well since we need to create a list to store the keys before converting it to a tuple.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...