How to Convert Tuple to Dictionary in Python

The dict() function in Python creates a dictionary. A dictionary is an iterable collection object that is unordered, changeable, and indexed.

Python tuple() is a built-in function used to create a tuple. A tuple is an immutable sequence type.

Converting one data type to another is a common operation, and in this tutorial, we will see how to convert a tuple to a dictionary. We will also see how to convert a list of tuples into a dictionary.

Python tuple to dictionary

To convert a tuple to dictionary in Python, use the dict() method. The dict() function takes a tuple of tuples as an argument and returns the dictionary. Each tuple contains a key-value pair.

A dictionary object can be constructed using a dict() function. 

tup = ((11, "eleven"), (21, "mike"), (19, "dustin"), (46, "caleb"))

dct = dict((y, x) for x, y in tup)


((11, 'eleven'), (21, 'mike'), (19, 'dustin'), (46, 'caleb'))
{'eleven': 11, 'mike': 21, 'dustin': 19, 'caleb': 46}

In this example, we used a dict() method and passed the dictionary comprehension as an argument to create a dictionary.

Dictionary comprehension is a method for transforming one dictionary into another dictionary.

During this transformation, elements within the original dictionary can be conditionally included in a new dictionary, and each element can be transformed as needed.

In the output, we get the key-value paired dictionary. This is because the first element of a tuple becomes a key and the second element becomes a dictionary’s value.

Using dict(), map() and reversed() method

You can use the combination of the dict(), map(), and reversed() methods to convert a tuple to the dictionary.

The map() method returns a map object, which is an iterator. The function returns the reversed iterator of the given sequence.

tup = ((11, "eleven"), (21, "mike"), (19, "dustin"), (46, "caleb"))

dct = dict(map(reversed, tup))


((11, 'eleven'), (21, 'mike'), (19, 'dustin'), (46, 'caleb'))
{'eleven': 11, 'mike': 21, 'dustin': 19, 'caleb': 46}

In this example, we got the dictionary, but here, the first element of the tuple becomes a value, and the second element becomes the key to the dictionary.

You can follow either this way or the above dictionary comprehension method based on your requirement.

Convert a list of Tuples into Dictionary

To convert a list of tuples into a dictionary, use the setdefault() method. The setdefault() method takes the first parameter to the key and the second parameter to a value of the dictionary.

The setdefault() function searches for a key, displays its value and creates a new key with def_value if it is not present. See the following code.

def conversion(tup, dict):
    for x, y in tup:
        dict.setdefault(x, []).append(y)
    return dict

tups = [("Boba", 21), ("Din", 19), ("Grogu", 46), ("Ahsoka", 11)]

dictionary = {}
print(conversion(tups, dictionary))


{'Boba': [21], 'Din': [19], 'Grogu': [46], 'Ahsoka': [11]}

You can see that it returns a dictionary that contains a key as the first element of a tuple, and the value is inside a list.

That is it for converting the tuple to a dictionary in Python.

See also

Python tuple to list

Python list to tuple

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.