How to convert Python tuple to dictionary

In this Python tutorial, we will discuss how to convert Python tuple to dictionary using different methods and examples.

In Python, we can convert Python tuple to dictionary using the following 5 methods

  1. Using for loop
  2. Using dict() function
  3. Using dictionary comprehension
  4. Using zip() & dict()
  5. Using namedtuple

How to convert Python tuple to dictionary

Let us discuss all 5 methods to convert a tuple to a dictionary in Python. And first, we will start with the method of using for loop

Method 1: How to convert Python tuple to dictionary using for loop

In this section, we will understand the use of for loop to convert the Python tuple to a dictionary.

An example related to this approach is given below.

# Defining a tuple 
countries = (
    ('United States', 21), 
    ('Canada', 34), 
    ('United Kingdom', 54), 
    ('China', 11)
)

# Defining a empty dictionary
countries_dict = {}

# Converting tuple to dict using for loop
for key, value in countries:
    countries_dict[key] = value
    
# Printing tuple
print('Tuple:', countries)

# Printing dictionary
print('Dictionary:', countries_dict)

In the above example, first, we defined a tuple named countries. Then we defined an empty dictionary named countries_dict.

Next, we utilized the for loop to iterate over each tuple pair and define them as a dictionary key-value pair. Once we execute the above Python program, we will get the following result.

Tuple: (('United States', 21), ('Canada', 34), ('United Kingdom', 54), ('China', 11))
Dictionary: {'United States': 21, 'Canada': 34, 'United Kingdom': 54, 'China': 11}

Read: 16 ways to convert a tuple to list in Python

Method 2: How to convert Python tuple to dictionary using dict()

Another method to convert a tuple to a dictionary in Python is by using the dict() function.

An example of using the dict() function on the tuple is given below.

# Defining a tuple 
usa_states = (
    ('Texas', 21), 
    ('Florida', 34), 
    ('Hawaii', 54), 
    ('California', 11)
)

# Converting tuple to dict using dict()
states_dict = dict(usa_states)

# Printing tuple
print('Tuple:', usa_states)

# Printing dictionary
print('Dictionary:', states_dict)

In the example, we have passed the usa_stated tuple to the dict() function to convert it into a dictionary. After this, we are assigning the dictionary to the states_dict variable.

The result of the above Python program is given below.

Tuple: (('Texas', 21), ('Florida', 34), ('Hawaii', 54), ('California', 11))
Dictionary: {'Texas': 21, 'Florida': 34, 'Hawaii': 54, 'California': 11}

Read: Convert tuple to string in Python

Method 3: How to convert Python tuple to dictionary using dictionary comprehension

In Python, dictionary comprehension is another way to convert a tuple to a dictionary.

Here we defined a tuple named usa_states containing more tuple pairs. Then we used list comprehension where we used for loop to iterate over each tuple pair and form a dictionary from it.

# Defining a tuple 
usa_states = (
    ('Texas', 21), 
    ('Florida', 34), 
    ('Hawaii', 54), 
    ('California', 11)
)

# Converting tuple to dict using dictionary comprehension
states_dict = {key: value for key, value in usa_states}
    
# Printing tuple
print('Tuple:', usa_states)

# Printing dictionary
print('Dictionary:', states_dict)

After executing the above Python program, we will get the following result.

Tuple: (('Texas', 21), ('Florida', 34), ('Hawaii', 54), ('California', 11))
Dictionary: {'Texas': 21, 'Florida': 34, 'Hawaii': 54, 'California': 11}

Read: Python dictionary list of tuples

Method 4: How to convert Python tuple to dictionary using zip() & dict()

Next, we can also use the zip() function in combination with the dict() function to convert the tuple to the dictionary.

The zip() function in Python takes multiple iterables as input, combines them, and returns the iterators of a tuple. After this, we can convert the tuple to a dictionary using the dict() function in Python.

An example of this approach is given below.

# Defining 2 tuples
usa_states = ('Texas', 'Florida', 'Hawaii', 'California')

state_scores = (72, 86, 65, 87)

# Using dict() and zip() to convert tuples to dictionary
state_score_dict = dict(zip(usa_states, state_scores))

# Printing tuple values
print('Keys Tuple:', usa_states)
print('Values Tuple:', state_scores)

# Printing dictionary
print('Dictionary:', state_score_dict)

In this example, we have taken 2 tuples, combine them using the zip() function, and convert them into a dictionary using dict().

Once we execute the Python program, we will get the following result.

Keys Tuple: ('Texas', 'Florida', 'Hawaii', 'California')
Values Tuple: (72, 86, 65, 87)
Dictionary: {'Texas': 72, 'Florida': 86, 'Hawaii': 65, 'California': 87}

Read: Python concatenate tuples

Method 5: How to convert Python tuple to dictionary using the namedtuple._asdict()

  • The _asdict() is a method in Python’s namedtuple class which comes under the collections module.
  • This method allows us to convert a named tuple into a dictionary. Moreover, the result of the dictionary is an ordered dictionary.
  • This means the order of the elements in the named tuple will be preserved in the dictionary.
# Importing namedtuple
from collections import namedtuple

# Defining a named tuple
usa_states = namedtuple('usa_states', ['Texas', 'Florida', 'California'])
state_scores = usa_states(72, 65, 98)

# Converting namedtuple to dictionary
state_dict = state_scores._asdict()

# Printing dictionary
print("Named Tuple:",state_scores)
print("Dictionary:",state_dict)

In the example, we first defined a named tuple and then we use the _asdict() method to convert the named tuple to an ordered dictionary.

The result of the above Python program is shown below.

Convert Python tuple to dictionary
Converting a Python tuple to dictionary

You may also like to read the following Python tutorials.

Conclusion

So, in this Python tutorial, we understood how to convert a Python tuple to a dictionary using 5 different methods. Moreover, for each method, we have illustrated different examples.

Here is the list of methods that we covered.

  1. How to convert Python tuple to dictionary using for loop
  2. How to convert Python tuple to dictionary using dict() function
  3. How to convert Python tuple to dictionary using dictionary comprehension
  4. How to convert Python tuple to dictionary using zip() & dict()
  5. How to convert Python tuple to dictionary using namedtuple