Home » Tuple to Dictionary in Python

Tuple to Dictionary in Python

by Online Tutorials Library

Tuple to Dictionary in Python

In this tutorial, we will discuss how we can convert a tuple to a dictionary in Python.

We know that the elements of tuples are enclosed within parentheses and the elements of a dictionary are present in the form of a key-value pair and are enclosed within curly brackets.

We will use the following techniques to convert a tuple to a dictionary in Python-

  1. Using setdefault()
  2. Using dict()
  3. Using Dictionary Comprehension
  4. Using zip() and dict()

Let’s get started with the first one,

Using setdefault()

The function of setdefault() is to return the value associated with a key and if the key is not present it is inserted with a default value.

The following program illustrates how it can be used in a Python program.

Output:

The converted dictionary is: {'English': [2001], 'Hindi': [2002], 'Mathematics': [2003], 'Computer Science': [2004], 'Physics': [2005], 'Chemistry': [2006]}  

Explanation-

  1. In the first step, we have created a function that takes tuple and dictionary as an input.
  2. After this, we have used for loop to use the setdefault() and append the subject name and the subject code.
  3. Now we have initialized the values of our tuple and declared the resultant dictionary as {}.
  4. On executing the above program, the expected output is displayed.

In the second program, we will learn how dict() can be used for the same.

Using dict()

The dict() is used to create a dictionary in Python, let’s see how it can add meaning to our program.

Consider the program given below,

Output:

The converted dictionary is: {'English': 2001, 'Hindi': 2002, 'Mathematics': 2003, 'Computer Science': 2004, 'Physics': 2005, 'Chemistry': 2006}  

Explanation-

  1. In the first step, we have created a function that takes tuple and dictionary as an input.
  2. After this, we have used for loop to use the dict() that takes tuple as a parameter and returns a dictionary.
  3. Now we have initialized the values of our tuple and declared the resultant dictionary as {}.
  4. On executing the above program, the expected output is displayed.

In the third program, we will see how dictionary comprehension can help us.

Using Dictionary Comprehension

The program below shows the same,

Output:

The values in sub_names are: ('English', 'Hindi', 'Mathematics', 'Computer Science', 'Physics', 'Chemistry')  The values in sub_codes are: (2001, 2002, 2003, 2004, 2005, 2006)  The resultant dictionary is: {'English': 2001, 'Hindi': 2002, 'Mathematics': 2003, 'Computer Science': 2004, 'Physics': 2005, 'Chemistry': 2006}  

Explanation-

  1. Firstly, we have initialized the two tuples, sub_names, and sub_codes, and displayed them.
  2. After this, the decision-making keyword, if is used to check whether the length of both the tuples is the same or not, if it is the same then the functionality defined in the dictionary comprehension is performed.
  3. On executing the given program, the desired output is displayed.

In the last program, we will learn how zip() and dict() can be used in the Python program.

Using zip() and dict()

We have understood how the dict() works, here we will be applying both dict() and zip(), the zip() method takes the iterable items and appends them to form a single tuple.

The following program illustrates the same-

Output:

The values in sub_names are: ('English', 'Hindi', 'Mathematics', 'Computer Science', 'Physics', 'Chemistry')  The values in sub_codes are: (2001, 2002, 2003, 2004, 2005, 2006)  The resultant dictionary is: {'English': 2001, 'Hindi': 2002, 'Mathematics': 2003, 'Computer Science': 2004, 'Physics': 2005, 'Chemistry': 2006}  

Explanation-

  1. Firstly, we have initialized the two tuples, sub_names, and sub_codes, and displayed them.
  2. After this, the decision-making keyword, if is used to check whether the length of both the tuples is the same or not, if it is the same then the functionality involving zip() and dict() is performed.
  3. On executing the given program, the desired output is displayed.

Conclusion

In this tutorial, we learned the different methods of converting a tuple to a dictionary in Python.


You may also like