Tuple as Dictionary Key in Python

How to have a tuple as a dictionary key in Python? Most of the time we keep the keys of a dictionary as a String but sometimes you may want to keep the complex objects like lists, tuples, or any other object as keys. When using a tuple, each tuple key contains multiple elements, which serve as a composite key for the dictionary.

You can tuple as a dictionary key in Python by using many ways, for example, by using dictionary comprehension, zip(), and dict() + dictionary comprehension. In this article, I will explain tuples as a dictionary key by using all these methods with examples.

1. Quick Examples of Tuple as Dictionary Key

If you are in a hurry, below are some quick examples of python tuples as dictionary key conversion.


# Quick examples of tuple Key dictionary conversion 

# Initializing list
list_tuple = [('Pytho', '35ddays', 2000), ('Spark', '40days' ,2500 ), ('Java', '50days', 3000)] 
 
# Example 1: Using list comprehension
# Tuple as dictionary key 
result = {(sub[0], sub[1]): sub[2:] for sub in list_tuple}
print("Dictionary after conversion : ", result)

# Example 2: Tuple as dictionary key 
# Using the zip() Function
keys = [(sub[0], sub[1]) for sub in list_tuple]
values = [sub[2:] for sub in list_tuple]
result = dict(zip(keys, values))

# Example 3: Using dict() + dictionary comprehension
result = dict(((index[0], index[1]), index[2:]) for index in list_tuple)

2. Tuple as Dictionary Key Using Dictionary Comprehension

You can convert a list of tuples to a dictionary using dictionary comprehension in Python. In order to do so, first initializes a list of tuples list_tuple, and then create a new dictionary result using dictionary comprehension. Each tuple in list_tuple has three elements, and the first two elements are used as key in the new dictionary, while the third element is used as the value. (sub[0], sub[1]) creates a tuple with the first and second elements of each tuple in list_tuple the key. sub[2:] creates a new tuple with the third element of each tuple list_tuple as the value. for sub in list_tuple iterates through each tuple in list_tuple.


# Initializing list
list_tuple = [('Pytho', '35ddays', 2000), ('Spark', '40days' ,2500 ), ('Java', '50days', 3000)] 
print("Original list:  ", list_tuple)
 
# Using list comprehension
# Tuple as dictionary key 
result = {(sub[0], sub[1]): sub[2:] for sub in list_tuple}
print("Dictionary after conversion : ", result)

Yields below output

Tuple as Dictionary Key Python

3. Tuple as Dictionary Key Using the zip() Function

You can also use the tuple as a dictionary key using the zip() function. For example, The keys variable is initialized using a list comprehension that extracts the first two elements of each tuple list_tuple and stores them as a tuple in the keys list.

The values variable is also initialized using a list comprehension that extracts all elements after the first two in each tuple and stores them in a list within the values list.

The zip() function is then used to combine the keys and values lists into a dictionary named result. The zip() function takes two or more iterables as input and returns an iterator that aggregates elements from each of the iterables. In this case, the keys and values lists are passed as input to the zip() function.


# Initializing list
list_tuple = [('Pytho', '35ddays', 2000), ('Spark', '40days' ,2500 ), ('Java', '50days', 3000)] 
print("Original list:  ", list_tuple)

# Tuple as dictionary key 
# Using the zip() function
keys = [(sub[0], sub[1]) for sub in list_tuple]
values = [sub[2:] for sub in list_tuple]
result = dict(zip(keys, values))
print("Dictionary after conversion : ", result)

Yields the same output as above.

4. Tuple as Dictionary Key Using dict() + Dictionary Comprehension

You can use dict() along with dictionary comprehension to convert a tuple dictionary key. For example, For example, to create the key tuple, you use the syntax (index[0], index[1]), which extracts the first two elements of each tuple in list_tuple and combines them into a tuple.

To create the value list, you use the syntax index[2:], which extracts all elements after the first two in each tuple list_tuple and creates a list containing those elements. To convert a list of tuples into a dictionary using a dictionary comprehension and the dict() function. The resulting dictionary can be useful for organizing data into a more structured format.


# initializing list
list_tuple = [('Pytho', '35ddays', 2000), ('Spark', '40days' ,2500 ), ('Java', '50days', 3000)] 
print("Original list:  ", list_tuple)

# Tuple as dictionary key 
# Using dict() + Dictionary Comprehension
result = dict(((index[0], index[1]), index[2:]) for index in list_tuple)
print("Dictionary after conversion : ", result)

Yields the same output as above.

Conclusion

In this article, I have explained how to use tuple as a dictionary key in Python by using dictionary comprehension, zip(), and dict() + dictionary comprehension with examples.

Happy Learning !!

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply