Python - Unpacking Dictionary Keys into Tuple


Python is a very commonly used programming language used by programmers spread all over the world. Python has many distinct applications, including web development, data science, machine learning, and the automation of various tasks. It offers a wide range of functions that may be utilised as needed. The ability of Python to unpack dictionary keys into tuples is one such capability. Multiple sets of data are stored using tuples in the form of a single variable. This article teaches us how to decompress a dictionary key into a tuple.

Unpacking

When you unpack, you remove various entries from a dictionary or list and give them various variables so that you may change the values as necessary. This approach is typical for both lists and dictionary keys.The basic syntax of unpacking the dictionary keys into tuples: -

unpacking_variable = tuple(dictionary.keys()) # The key method is used to return a view object that contains keys for the dictionary

Different Techniques of Unpacking Dictionary Keys into Tuple

Basic Tuple

This is the most basic method of converting the dictionary keys into tuple. Let’s take an example to understand this method in a better way:

Example

Details = {
    'Name': 'Sam',
    'Age': 12,
    'Standard': '5'
} # This is the dictionary which to be unpacked

unpacking_tuple = tuple(Details.keys()) # With the help of keys() we get the keys of dictionary and convert it into tuple
print(unpacking_tuple) # The tuple is displayed

Output

The output of the above example will be as follows:

('Name', 'Age', 'Standard') 

Multiple Variables Unpacking

When the need is arisen to keys to multiple variables then in such a case, unpacking the dictionary into tuple becomes more useful. Lets take an example to understand it in a better way:

Example

Details = {
    'Name': 'Sam',
    'Age': 12,
    'Standard': 5
} # This is the dictionary to be unpacked

Name, Age, Standard = Details.keys() # The dictionary is unpacked into three different variables
print(f"Name: {Name}, Age: {Age}, Standard: {Standard}") 

Output

The output of the above example is as follows:

Name: Name, Age: Age, Standard: Standard

Iterating Over Dictionary

It becomes very easy to iterate over each key in the dictionary when the dictionary is unpacked into a tuple. Let’s take an example to understand it in a better way:

Example

Details = {
    'Name': 'Sam',
    'Age': 12,
    'Standard': '5'
} # This is the dictionary that is to be unpacked 

for key in Details.keys(): # For loop is used to iterate over each key in the dictionary
    print(f"Key: {key}, Value: {Details[key]}")

Output

The output of the above will be as follows:

Key: Name, Value: Sam
Key: Age, Value: 12
Key: Standard, Value: 5

Sorting Keys

Unpacking the dictionary's keys into a tuple makes it simple to sort them as necessary. Let’s take an example of this method to understand it in a more better way:

Example

Details = {
    'Name': 'Sam',
    'Age': 12,
    'Standard': '5'
} # This is the dictionary that is to be unpacked 

unpacked_keys = sorted(Details.keys()) # Sorted function will be used to sort the keys
print(unpacked_keys) # The sorted keys will be displayed as output

Output

The output of the example will be as follows:

['Age', 'Name', 'Standard'] 

Extracting Values

We can easily get the value of a particular key by unpacking the dictionary keys into tuple. Let’s take an example to understand it in a better way:

Example

Details = {
    'Name': 'Sam',
    'Age': 12,
    'Standard': '5'
} # This is the dictionary that is to be unpacked 

name, age, standard = [Details[key] for key in ('Name', 'Age', 'Standard')] # We will use the list comprehension to get the values of keys
print(f"Name: {name}, Age: {age}, Standard: {standard}")

Output

The output of the above example is as follows:

Name: Sam, Age: 12, Standard: 5 

Ignoring Unwanted Keys

There are cases when we don’t need all the keys present in the dictionary. So, in this case some of the keys are ignored while unpacking the dictionary keys into tuple. Let’s take an example to understand it in a better way:

Example

Details = {
    'Name': 'Sam',
    'Age': 12,
    'Standard': '5',
    'Country' : 'USA'
} # This is the dictionary that is to be unpacked

name, age,  *other_info = Details.keys()
print(f"Name: {name}, Age: {age}") # The required keys are unpacked separately
print(f"Other Info: {other_info}") # The other keys are unpacked separately

Output

The output of the above example is as follows:

Name: Name, Age: Age
Other Info: ['Standard', 'Country']

Conclusion

A versatile and effective technique to work with dictionary keys is to unpack the keys into tuples. We may simply edit, iterate over, or assign the keys to several variables by transforming them into a tuple. The examples given in this article demonstrate how unpacking dictionary keys may be used in real−world situations and highlight how valuable and convenient it is for Python programming.

One can use any of the above example as a reference to work on their personal code as per requirement and it is also very necessary to have knowledge about all the different techniques to unpack the dictionary keys into tuple and their advantages as this may help them during any part of their programming career and help them become an efficient programmer.

Updated on: 01-Aug-2023

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements