
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Python Tuple into Dictionary
The Python tuple elements are enclosed inside the parentheses, while dictionary elements are present in the form of a key-value pair and are enclosed between curly brackets.
In this article, we will show you how to convert a Python tuple into a dictionary. The following are the methods to convert a tuple into a dictionary -
Assume we have taken a tuple containing some elements. We will convert the input tuple into a Python dictionary and return it using different methods as specified above.
Using dict() function
In Python, use the dict() function to convert a tuple to a dictionary. A dictionary object can be created with the dict() function. The dictionary is returned by the dict() method, which takes a tuple of tuples as an argument. A key-value pair is contained in each tuple.
Here in the code below, to create a dictionary, we used the dict() method and gave a dictionary comprehension as an argument. Dictionary comprehension is a technique for converting one dictionary into another. Elements from the original dictionary can be conditionally included in the new dictionary throughout this conversion, and each element can be converted as needed.
The output is a key-value paired dictionary. The first element of a tuple becomes a dictionary key, while the second element of a tuple becomes a dictionary value.
Example
The following program converts a tuple into a dictionary using the dict() function -
# input tuple inputTuple = ((5, "TutorialsPoint"), (6, "Python"), (7, "Codes")) print("The input Tuple:", inputTuple) # Here we are iterating through each element (pairs) of the tuple using dictionary comprehension and converting it to the dictionary resultDictionary = dict((x, y) for x, y in inputTuple) print("The result dictionary:", resultDictionary)
Executing the above program will generate the following output -
The input Tuple: ((5, 'TutorialsPoint'), (6, 'Python'), (7, 'Codes')) The result dictionary: {5: 'TutorialsPoint', 6: 'Python', 7: 'Codes'}
Using Dictionary Comprehension and enumerate () Function
To convert two tuples into a dictionary, the tuples must be the same length. Otherwise, we will not be able to match all of the key-value pairs.
The enumerate() method is basically used to add a counter to an iterable and returns the enumerate object. Here is the syntax for the enumerate() function -
enumerate(iterable, start=0)
Where,
- iterable: It can be any sequence/object/iterable supporting iteration.
- start: enumerate() begins counting from this value. If start is not specified, the value 0 is used.
Example
The following program converts both tuples into a dictionary using the dictionary comprehension method(one tuple as keys and the other as values of the dictionary) -
# input tuple_1 inputTuple_1 = ('TutorialsPoint', 'Python', 'Codes') # input tuple_2 inputTuple_2 = (5, 6, 7) # printing the input tuple_1(keys) print("The input Tuple_1(keys) = ", inputTuple_1) # printing the input tuple_2(values) print("The input Tuple_2(values) = ", inputTuple_2) # Checking whether the length of both the tuples are equal or not if len(inputTuple_1) == len(inputTuple_2): # converting both the tuples into a dictionary using enumerate() # function in a dictionary comprehension resultDictionary = {inputTuple_1[i] : inputTuple_2[i] for i, _ in enumerate(inputTuple_2)} # printing the result dictionary from the given two tuples print("The result dictionary:", resultDictionary)
When you run the program, it will show this output -
The input Tuple_1(keys) = ('TutorialsPoint', 'Python', 'Codes') The input Tuple_2(values) = (5, 6, 7) The result dictionary: {'TutorialsPoint': 5, 'Python': 6, 'Codes': 7}
Using zip() and dict() Functions
The zip() function is basically used to combine two or more iterables in a single iterable. The items of the input iterables are paired together as per their index positions.
For example, If you have two lists of the same length, the zip() method creates a new list of tuples with one item from each of the input lists at the same index. Here is the syntax for zip() function -
Example
The following program converts both tuples into a dictionary using the zip() and dict() functions (one tuple as keys and the other as values of the dictionary) -
# input tuple_1 inputTuple_1 = ('TutorialsPoint', 'Python', 'Codes') # input tuple_2 inputTuple_2 = (5, 6, 7) # printing the input tuple_1(keys) print("The input Tuple_1(keys) = ", inputTuple_1) # printing the input tuple_2(values) print("The input Tuple_2(values) = ", inputTuple_2) # Checking whether the lengths of both the tuples are equal or not if len(inputTuple_1) == len(inputTuple_2): # converting both the tuples into a dictionary using zip() # and dict() functions # Here zip function takes elements of input tuple 1 as keys and input tuple 2 elements as values # Then we convert this to a dictionary using dict() resultDictionary = dict(zip(inputTuple_1, inputTuple_2)) # printing result dictionary from the given two tuples print("The result dictionary:", resultDictionary)
After running the program, you will get this result -
The input Tuple_1(keys) = ('TutorialsPoint', 'Python', 'Codes') The input Tuple_2(values) = (5, 6, 7) The result dictionary: {'TutorialsPoint': 5, 'Python': 6, 'Codes': 7}