Convert a list of tuples to a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Jun 30, 2022

banner

Check out my new book

Convert a list of tuples to a dictionary in Python #

Use the dict class to convert a list of tuples to a dictionary, e.g. my_dict = dict(list_of_tuples). The dict class can be passed a list of tuples and returns a new dictionary.

main.py
list_of_tuples = [('one', 1), ('two', 2), ('three', 3)] my_dict = dict(list_of_tuples) # 👇️ {'one': 1, 'two': 2, 'three': 3} print(my_dict)

We used the dict class to convert a list 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 list of tuples to a dictionary, e.g. my_dict = {tup[0]: tup[1] for tup in list_of_tuples}. The dict comprehension will iterate over the list allowing us to set each key-value pair to specific tuple elements.

main.py
list_of_tuples = [('one', 1), ('two', 2), ('three', 3)] my_dict = {tup[0]: tup[1] for tup in list_of_tuples} # 👇️ {'one': 1, 'two': 2, 'three': 3} 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 in the list have only 2 elements - a key and a value.

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

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

main.py
list_of_tuples = [('one', 1), ('two', 2), ('three', 3)] my_dict = {key: value for key, value in list_of_tuples} 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
list_of_tuples = [(1, 'one'), (2, 'two'), (3, 'three')] my_dict = {key: value for value, key in list_of_tuples} print(my_dict) # 👉️ {'one': 1, 'two': 2, 'three': 3}
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 = ('one', 1) print(key) # 👉️ one print(value) # 👉️ 1

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 = ('one', 1, 'a')
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, _ = ('one', 1, 'a') print(key) # 👉️ one print(value) # 👉️ 1

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.

Use the search field on my Home Page to filter through my more than 3,000 articles.