How to Convert Python Tuple to Dictionary
The dict() function in Python creates a dictionary. A dictionary is an iterable collection object that is unordered, changeable, and indexed. Python tuple() is an inbuilt function that is 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 Python tuple to dictionary. We will also see how to convert a list of tuples into the dictionary.
Convert Python Tuple to Dictionary
To convert a tuple to dictionary in Python, use the dict() method. A dictionary object can be constructed using a dict() function. The dict() function takes a tuple of tuples as an argument and returns the dictionary. Each tuple contains a key-value pair.
tup = ((11, "eleven"), (21, "mike"), (19, "dustin"), (46, "caleb")) print(tup) dct = dict((y, x) for x, y in tup) print(dct)
Output
((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. The first element of a tuple becomes a key and the second element of a tuple becomes a value of a dictionary.
Using dict(), map() and reversed() method
You can use the combination of dict(), map(), and reversed() method to convert a tuple to the dictionary. The map() method returns a map object, which is an iterator. The reversed() function returns the reversed iterator of the given sequence.
tup = ((11, "eleven"), (21, "mike"), (19, "dustin"), (46, "caleb")) print(tup) dct = dict(map(reversed, tup)) print(dct)
Output
((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 of a tuple becomes the key to the dictionary. Based on your requirement, you can follow either this way or the above dictionary comprehension method way.
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 key and the second parameter to a value of the dictionary.
The setdefault() function searches for a key and displays its value, and creates a new key with def_value if the key 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))
Output
{'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 tuple to dictionary in Python.