Python – Create Dictionary Of Tuples

In this Python tutorial, we will understand how to create a Python Dictionary of tuples. Additionally, we will also see how to create a dictionary of tuples in Python by using tuples as values.

In Python, we can create a dictionary of tuples using the following 6 methods.

  1. Using the square brackets
  2. Using the dictionary comprehension
  3. Using the zip() function
  4. Using the fromkeys() Method
  5. Using the list of tuples
  6. Using the setdefault() method

Python Dictionary of tuples

Here we will discuss all 6 methods for creating a dictionary of tuples in Python. So, let us start with the first method which includes the use of square brackets.

Method 1: Python Dictionary of tuples using square brackets

In this method, first, we will create an empty dictionary either by using the square brackets [] or by using the dict() function.

Once we create an empty dictionary, we will use the square brackets to define a key of the dictionary. And to define values for each key, we can use a tuple.

Here is an example of this approach in Python.

# Create an empty dictionary
usa_dict = {}

# Add key-value pairs to the dictionary
usa_dict['California'] = ('San Francisco', 'Los Angeles')
usa_dict['New York'] = ('New York City', 'Buffalo')
usa_dict['Texas'] = ('Houston', 'Austin')

# Print the dictionary
print(usa_dict)

In the example, we created a dictionary named usa_dict. The dictionary keys are the state names and each key value is a tuple containing multiple city names from the same state.

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

{'California': ('San Francisco', 'Los Angeles'), 'New York': ('New York City', 'Buffalo'), 'Texas': ('Houston', 'Austin')}

Bonus: Python Dictionary Methods

READ:  Python Program for Binary Search

Method 2: Python Dictionary of tuples using dictionary comprehension

Dictionary comprehension in Python is another quick way to create a dictionary of tuples.

In this method, we can define a few lists which we can use in the dictionary comprehension to create a dictionary of tuples.

Here is an example of this in Python.

# Defining lists
keys = ['California', 'New York', 'Texas']
values = [2, 6, 9]

# Using dictionary comprehension
usa_states_dict = {keys[i]: (values[i], values[i]*2) for i in range(len(keys))}

# Printing dictionary of tuples
print(usa_states_dict)

In this example, we first defined 2 lists of keys and values respectively. The keys list is utilized as keys in the dictionary and the values list is used to generate a tuple for each key.

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

{'California': (2, 4), 'New York': (6, 12), 'Texas': (9, 18)}

Read: Python dictionary of lists

Method 3: Python Dictionary of tuples using the zip() function

Another common way to create a dictionary of tuples in Python is by using the zip() function.

  • In this approach, we will define all the elements separately as lists and then we will use the zip() function to combine all elements to form a list of tuples.
  • Once we have the list of tuples, we will use the dict() function to convert the list of tuples to the dictionary of tuples.

Here is an example.

# Defining lists
fruits = ['apple', 'banana', 'orange']
colors = ['red', 'yellow', 'orange']
quantities = [1, 2, 3]

# Using zip() function to combine the lists into a list of tuples
fruits_data = list(zip(fruits, colors, quantities))

# Converting the list of tuples to a dictionary of tuples
my_dict = dict((f, (c, q)) for f, c, q in fruits_data)

# Print the dictionary
print(my_dict)
  • In the example, we defined 2 different lists in Python. After this, we used the list() and zip() functions to create a list of tuples using it.
  • In the last, we passed the list of tuples to the dict() function in such a way that it converts the list of tuples to a dictionary of tuples.

Here is the final result of the Python program.

{'apple': ('red', 1), 'banana': ('yellow', 2), 'orange': ('orange', 3)}

Read: Python dictionary multiple keys

READ:  How to Create String with Single Quotes in Python

Method 4: Python Dictionary of tuples using fromkeys() Method

The fromkeys() in Python is a built-in dictionary method that helps to create a dictionary with the same default value for each key.

So, here we will use the fromkeys() dictionary method to create a dictionary in which every key will have the same tuple as the default value.

Here is an example of this execution in Python.

fruits = ['apple', 'banana', 'orange']

# Creating a new dictionary using fromkeys() method
my_dict = dict.fromkeys(fruits, (1, 'Unknown'))

print(my_dict)

Here we created a list that will be used to generate keys for the dictionary. And then, we used the fromkeys() method to define a tuple value for each key.

The result of the above Python program is shown below.

{'apple': (1, 'Unknown'), 'banana': (1, 'Unknown'), 'orange': (1, 'Unknown')}

Read: Get all values from a dictionary Python

Method 5: Python Dictionary of tuples using the list of tuples

Next, let us see how to create a Python dictionary of tuples using the list of tuples. Basically, a list of tuples includes a list containing multiple tuple values.

However, we create a dictionary of tuples using it just by formatting and converting it to a dictionary using dict() function.

Here is an example of this task in Python.

# Creating a list of tuples
usa_data = [
    ('California', 'San Francisco', 'Los Angeles'), 
    ('New York', 'New York City', 'Buffalo'), 
    ('Texas', 'Houston', 'Austin')]

# Creating a dictionary of tuples using the list of tuples
usa_dict = dict((x, (y, z)) for x, y, z in usa_data)

# Printing the dictionary
print(usa_dict)

In this example, we defined a list of tuples where each tuple consists of 3 values. So, we used the for loop inside the dict() function to fetch each value and format it as a dictionary of tuples.

READ:  Python select from a list + Examples

Here is the final result of the above Python program.

{'California': ('San Francisco', 'Los Angeles'), 'New York': ('New York City', 'Buffalo'), 'Texas': ('Houston', 'Austin')}

Method 6: Python Dictionary of tuples using the setdefault() method

Just like fromkeys(), the setdefault() is another built-in Dictionary method in Python. This method allows specifying a default value for each key only for a key that does not already exist in the dictionary.

So, just like fromkeys(), we will also use the setdefault() in Python.

# Create an empty dictionary
usa_dict = dict()

# Creating a list
cities = ['San Francisco', 'Los Angeles', 'San Diego']

# Creating a new dictionary using setdefault() method
for city in cities:
    usa_dict.setdefault(city, (1, 'California'))

# Printing the dictionary 
print(usa_dict)

In this example, we first created an empty dictionary using the dict() function. After this, we defined a Python list containing city names.

Next, we used the for loop to iterate over each list value and used that value in the setdefault() as the key. And for each key, we specified the same tuple.

The final dictionary of tuples is shown in the result below.

Python Dictionary of tuples
Creating a Python Dictionary of tuples

So, in this section, we have covered 6 different methods to create a dictionary of tuples in Python.

You may also like to read the following Python tutorials.

Conclusion

So, in this Python tutorial, we understood how to create a Python Dictionary of tuples using 6 different methods. Moreover, for each method, we covered a different example.

Here is the list of methods that we covered:

  1. Using the square brackets
  2. Using the dictionary comprehension
  3. Using the zip() function
  4. Using the fromkeys() Method
  5. Using the list of tuples
  6. Using the setdefault() method