Skip to content

Convert Tuples to Dictionary in Python Malli Spark By {Examples}

  • by

How to convert tuples to a dictionary in python? A tuple is an ordered collection of elements enclosed in parentheses and separated by commas, whereas a dictionary is an unordered collection of key-value pairs enclosed in curly braces, where each key-value pair is separated by a colon and the pairs are separated by commas.

You can convert tuples to a dictionary in python by using many ways, for example, using the for loop, dictionary comprehension, map(), and zip() + dict() functions. In this article, I will explain convert tuples into a dictionary by using all these methods with examples.

1. Quick Examples of Converting Tuples to Dictionary

If you are in a hurry, below are some quick examples of how to convert tuples to a dictionary in python.

# Quick examples of converting tuples to dictionary

# Initialize tuples
tuples1 = (‘Spark’, ‘Python’, ‘Pandas’)
tuples2 = (25000, 20000, 30000)

# Example 1: Convert tuples to the dictionary
result = {}
for i in range(len(tuples1)):
result[tuples1[i]] = tuples2[i]

# Example 2: Convert tuples to a dictionary
# Using dictionary comprehension
if len(tuples1) == len(tuples2):
result = {tuples1[i] : tuples2[i] for i, _ in enumerate(tuples2)}

# Example 3: Using map() function
# Convert tuples to dictionary
if len(tuples1) == len(tuples2):
result = dict(map(lambda x, y: (x, y), tuples1, tuples2))

# Example 4: Using zip() + dict() function
# Convert tuples to dictionary
if len(tuples1) == len(tuples2):
result = dict(zip(tuples1, tuples1))

2. Convert Tuples to Dictionary Using For Loop

You can convert two tuples to a dictionary using for loop in Python, we take two tuples tuples1 and tuples2 with corresponding values, and each element of tuples1 is paired with a corresponding element of tuples2 to convert them into a dictionary. In the dictionary, the values of tuple1 become keys and the values of tuples2 are dictionary values.

Iterate through the indices of tuples1 using range(len(tuples1)), and uses each index i to access the corresponding value in tuples1 using tuples1[i], and the corresponding value in tuples2 using tuples2[i]. It then adds this key-value pair to the dictionary using the syntax result[tuples1[i]] = tuples2[i].

# initializing tuples
tuples1 = (‘Spark’, ‘Python’, ‘Pandas’)
tuples2 = (25000, 20000, 30000)

print(“Original tuples1: “, tuples1)
print(“Original tuples2: “, tuples2)

# initialize empty dictionary
result = {}

# Convert tuples to dictionary using for loop
for i in range(len(tuples1)):
result[tuples1[i]] = tuples2[i]
print(“convert tuples to dictionary : “, result)

Yields below output.

3. Convert Tuples to Dictionary Using Dictionary Comprehension

You can use dictionary comprehension to convert a list of tuples into a dictionary. when using this first check if the length of tuples1 is equal to the length of tuples2. This is necessary because you want to create a dictionary where each element of tuples1 is paired with a corresponding element of tuples2. If the lengths of the tuples are equal, you can use dictionary comprehension to iterate over each index i and create a new key-value pair in the resulting dictionary. The key is the element at the index i in tuples1, and the value is the element at the index i in tuples2.

# initializing tuples
tuples1 = (‘Spark’, ‘Python’, ‘Pandas’)
tuples2 = (25000, 20000, 30000)

print(“Original tuples1: “, tuples1)
print(“Original tuples2: “, tuples2)

# Convert tuples to dictionary
# Using dictionary comprehension
if len(tuples1) == len(tuples2):
result = {tuples1[i] : tuples2[i] for i, _ in enumerate(tuples2)}
print(“convert tuples to dictionary : “, result)

Yields the same output as above.

4. Convert Tuples to Dictionary Using the map() Function

You can use the map() function to convert two tuples into a dictionary where elements of one tuple are keys and elements of the other tuple are values. For example, you first initialize two tuples tuples1 and tuples2 with some values. Then, you can use the map() function to iterate over the two tuples in parallel. The lambda function takes two arguments x and y, which correspond to the elements at the same index in the two tuples. The lambda function returns a tuple (x, y), which is then passed to the dict() function to create a new key-value pair in the resulting dictionary.

# initializing tuples
tuples1 = (‘Spark’, ‘Python’, ‘Pandas’)
tuples2 = (25000, 20000, 30000)

print(“Original tuples1: “, tuples1)
print(“Original tuples2: “, tuples2)

# Using map() function
# Convert tuples to dictionary
if len(tuples1) == len(tuples2):
result = dict(map(lambda x, y: (x, y), tuples1, tuples2))
print(“convert tuples to dictionary : “, result)

Yields the same output as above.

5. Convert Tuples to Dictionary Using zip() + dict() Function

You can use the zip() function along with the dict() function to convert two tuples into a dictionary where elements of one tuple are keys and elements of the other tuple value. For example, you first initialize two tuples tuples1 and tuples2 with some values. Then, you can use the zip() function to combine the two tuples into a list of tuples where each tuple contains two elements, the element at the same index tuples1 and the element at the same index in tuples2. You can pass the list of tuples to the dict() function to create a new dictionary with keys from tuples1 and values from tuples2.

# initializing tuples
tuples1 = (‘Spark’, ‘Python’, ‘Pandas’)
tuples2 = (25000, 20000, 30000)

print(“Original tuples1: “, tuples1)
print(“Original tuples2: “, tuples2)

# Using zip() + dict() function
# Convert tuples to dictionary
if len(tuples1) == len(tuples2):
result = dict(zip(tuples1, tuples1))
print(“convert tuples to dictionary : “, result)

Yields the same output as above.

Conclusion

In this article, I have explained how to convert tuples to a dictionary in python by using for loop, dictionary comprehension, map(), and zip() + dict() functions with examples. we took two tuples tuples1 and tuples2 with corresponding values, and each element of tuples1 is paired with a corresponding element of tuples2 to convert them into a dictionary.

Happy Learning !!

 How to convert tuples to a dictionary in python? A tuple is an ordered collection of elements enclosed in parentheses and separated by commas, whereas a dictionary is an unordered collection of key-value pairs enclosed in curly braces, where each key-value pair is separated by a colon and the pairs are separated by commas. You  Read More Python, Python Tutorial, python tuple examples 

Leave a Reply

Your email address will not be published. Required fields are marked *