Convert a tuple to a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Jul 2, 2022

banner

Photo from Unsplash

Convert a tuple to a dictionary in Python #

Use the dict class to convert a tuple to a dictionary, e.g. my_dict = dict(my_tuple). The dict class can be passed an iterable of keyword arguments (e.g. a tuple of tuples) and returns a new dictionary.

main.py
tuple_of_tuples = (('a', 'one'), ('b', 'two'), ('c', 'three')) my_dict = dict(tuple_of_tuples) # 👇️ {'a': 'one', 'b': 'two', 'c': 'three'} print(my_dict)

We used the dict class to convert a tuple of tuples to a dictionary.

Note that this approach only works if your tuples contain 2 elements each - a key and a value.

Alternatively, you can use a dict comprehension.

Use a dict comprehension to convert a tuple to a dictionary, e.g. my_dict = {tup[0]: tup[1] for tup in tuple_of_tuples}. The dict comprehension will iterate over the tuple allowing us to set each key-value pair to specific tuple elements.

main.py
tuple_of_tuples = (('a', 'one'), ('b', 'two'), ('c', 'three')) my_dict = {tup[0]: tup[1] for tup in tuple_of_tuples} # 👇️ {'a': 'one', 'b': 'two', 'c': 'three'} print(my_dict)

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary, or select a subset of key-value pairs that meet a condition.

On each iteration, we set the dictionary key to the first element in the tuple and the corresponding value to the second element.

This approach is much more flexible than using the dict class because it doesn't assume that the tuples have only 2 elements - a key and a value.

main.py
tuple_of_tuples = (('one', 'a', 1), ('two', 'b', 2), ('three', 'c', 3)) my_dict = {tup[1]: tup[0] for tup in tuple_of_tuples} # 👇️ {'a': 'one', 'b': 'two', 'c': 'three'} print(my_dict)

If your tuples only contain two elements, you can also use unpacking.

main.py
tuple_of_tuples = (('a', 'one'), ('b', 'two'), ('c', 'three')) my_dict = {key: value for key, value in tuple_of_tuples} # 👇️ {'a': 'one', 'b': 'two', 'c': 'three'} print(my_dict)

As opposed to the dict class, this approach would also work if the first element in each tuple is the value.

main.py
tuple_of_tuples = (('one', 'a'), ('two', 'b'), ('three', 'c')) my_dict = {key: value for value, key in tuple_of_tuples} # 👇️ {'a': 'one', 'b': 'two', 'c': 'three'} print(my_dict)
When unpacking, make sure to declare exactly as many variables as there are items in the iterable.

When unpacking from a tuple, each variable declaration counts for a single item.

Make sure to declare exactly as many variables as there are items in the tuple.

main.py
key, value = ('a', 'one') print(key) # 👉️ 'a' print(value) # 👉️ 'one'

If you try to unpack more or less values than there are in the tuple, you would get an error.

main.py
# ⛔️ ValueError: too many values to unpack (expected 2) key, value = ('a', 'one', 'two')
We declare 2 variables, but the tuple contains 3 items. The inconsistency between the number of variables and items in the tuple causes a ValueError.

If you don't need to store a certain value, use an underscore for the variable's name.

main.py
key, value, _ = ('a', 'one', 'two') print(key) # 👉️ 'a' print(value) # 👉️ 'one'

When you use an underscore for a variable's name, you indicate to other developers that this variable is just a placeholder.

You can use as many underscores as necessary when unpacking values.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.