How to Create a Dictionary Of Tuples in Python


This walkthrough is all about creating a dictionary of tuples in Python. This data structure stores key-value pairs. By combining dictionaries and tuples, a dictionary of tuples can be created. The benefit is organized and accessible data in a structured format. Becomes easy to represent multiple values for each key, such as student grades or contact information. Let us see how this efficiently stores and retrieves complex data.

Syntax

Ensure Python is installed on your system for its simplicity and readability. Create a dictionary of tuples using the following syntax:

dictionary_name = {key1: (value1_1, value1_2, ...), key2: 
(value2_1, value2_2, ...), ...}

Example

# Create a dictionary of students and their grades
students = {"John": (85, 90), "Emma": (92, 88), "Michael": (78, 80)}

# Accessing the values using keys
print(students["John"]) 
print(students["Emma"]) 
print(students["Michael"])  

Output

(85, 90)
(92, 88)
(78, 80)

Initiate a dictionary called students. The keys are student names and the values are tuples representing their grades.

Algorithm

  • Follow these steps to create a tuple dictionary:

  • Declare an empty dictionary.

  • Add the key as a dictionary key and the matching values as a tuple to each key-value pair.

  • Repeat this step for each key-value pair.

Once all of the key-value pairs have been added as tuples to the dictionary, the dictionary of tuples has been generated. Now it is ready for additional operations. To avoid overwriting any current values in the dictionary, keys must be unique.

Examples

# Create a dictionary of books and their authors
books = {"Harry Potter": ("J.K. Rowling", 1997), "To Kill a Mockingbird": 
("Harper Lee", 1960)}

# Adding a new book
books["1984"] = ("George Orll", 1949)

# Accessing the values using keys
print(books["Harry Potter"])  # Output: ("J.K. Rowling", 1997)
print(books.get("To Kill a Mockingbird"))  

Output

('J.K. Rowling', 1997)
('Harper Lee', 1960)

Here, a dictionary called books is built. The keys represent book titles and the values are tuples with the author and the publication year. You can add a new key-value pair to the dictionary as seen on line 3. This newly added value is accessible using indexing as well as the get() method.

Example

# capitals and country dict
countries = {"USA": ("Washington D.C.", 328.2), "France": 
("Paris", 67.06), "Japan": ("Tokyo", 126.5)}

# Removing a country
del countries["France"]

# Checking if a key exists
if "Japan" in countries:
   print("Japan is in the dictionary.")

# Iterating over the dictionary
for country, (capital, population) in countries.items():
   print(f"{capital} - {country} w/ {population} million.")

Output

Japan is in the dictionary.
Washington D.C. - USA w/ 328.2 million.
Tokyo - Japan w/ 126.5 million.

The del keyword removes a key-value pair from the dictionary. It is possible to verify if a key exists in the dictionary. If you wish to iterate through the dictionary, use the items() function.

Applications

A dictionary of tuples has applications in storage of employee records, product catalog management, educational setting, and event planning. It is useful in scenarios where one would store information like name, age, position, salary, and other relevant data, while also containing student grades and event details.

employees = {
   101: ('John Doe', 30, 'Software Engineer', 80000),
   102: ('Alice Smith', 28, 'Data Analyst', 60000),
   103: ('Bob Johnson', 35, 'Manager', 90000)
}

products = {
   'product1': ('Laptop', 1200, 'Electronics', 50),
   'product2': ('Shirt', 30, 'Apparel', 200),
   'product3': ('Book', 15, 'Books', 1000)
}

countries = {
   'USA': (331,002,651, 9833520, 'Washington D.C.'),
   'China': (1439323776, 9596961, 'Beijing'),
   'Brazil': (212559417, 8515767, 'Brasília')
}

grades = {
   'Alice': (85, 90, 78, 93),
   'Bob': (70, 80, 85, 75),
   'Charlie': (95, 88, 92, 89)
}

events = {
   'event1': ('2023-07-30', '10:00 AM', 'Conference Hall A', 'Workshop'),
   'event2': ('2023-08-15', '7:30 PM', 'Auditorium', 'Concert'),
   'event3': ('2023-09-05', '2:00 PM', 'Room 101', 'Seminar')
}

Conclusion

This article has dived deep in ways of creating a tuple dictionary in Python. To recap, construct a dictionary and populate it with tuples, use Python's basic data structure syntax. Specifying the keys and values for each tuple in the dictionary is part of the algorithm for building a dictionary of tuples. This adaptable data structure organizes and retrieves information rapidly. Experiment and practice to improve comprehension.

Updated on: 09-Aug-2023

344 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements