How to Convert Tuple to Dictionary in Python

There are the following methods to convert a tuple to a dictionary in Python.

  1. Method 1: Using the dict() function
  2. Method 2: Using dict(), map() and reversed() functions
  3. Method 3: Using a dictionary comprehension
  4. Method 4: Using the setdefault() function
  5. Method 5: Using the update() function
  6. Method 6: Using a for loop

Method 1: Using the dict() function

To convert a tuple to a dictionary in Python, you can 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.

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. This is because the first element of a tuple becomes the key, and the second element becomes a dictionary’s value.

Method 2: Using dict(), map() and reversed() functions

You can use the combination of the dict(), map(), and reversed() methods to convert a tuple to a 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"))
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 becomes the key to the dictionary.

Method 3: Using a dictionary comprehension

Dictionary comprehension is a concise way to create a dictionary in Python. It is similar to list comprehension but creates a dictionary instead of a list.

tuple_data = (('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3'))
dictionary = {key: value for key, value in tuple_data}

print("Converted dictionary:", dictionary)

Output

Converted dictionary: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

Method 4: Converting a list of Tuples into Dictionary

To convert a list of tuples into a dictionary, you can 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 absent.

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]}

It returns a dictionary containing a key as the first element of a tuple, and the value is inside a list.

Method 5: Using the update() function

The update() function is a method available for dictionaries in Python. It adds or updates key-value pairs from another dictionary or an iterable containing key-value pairs.

tuple_data = (('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3'))
dictionary = {}

for key, value in tuple_data:
  dictionary.update({key: value})

print("Converted dictionary:", dictionary)

Output

Converted dictionary: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

Method 6: Using a for loop

tuple_data = (('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3'))
dictionary = {}

for key, value in tuple_data:
  dictionary[key] = value

print("Converted dictionary:", dictionary)

Output

Converted dictionary: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

That’s it.

Leave a Comment

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