Open In App

Create Dictionary Of Tuples – Python

Last Updated : 12 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of creating a dictionary of tuples in Python involves mapping each key to a tuple of values, enabling structured data storage and quick lookups. For example, given a list of names like [“Bobby”, “Ojaswi”] and their corresponding favorite foods as tuples [(“chapathi”, “roti”), (“Paraota”, “Idly”, “Dosa”)], the goal is to generate a dictionary like {‘Bobby’: (‘chapathi’, ‘roti’), ‘Ojaswi’: (‘Paraota’, ‘Idly’, ‘Dosa’)}.

Using dictionary literal

This is the most straightforward method to define a dictionary where keys map to tuple values. It is best suited for small, predefined datasets where values do not change frequently. Additionally, Python allows tuples to be used as dictionary keys since they are immutable, making it useful for scenarios where fixed sets of values need mapping.

Python
# tuple of favourite food as key
# value is name of student
d = {("chapathi", "roti"): 'Bobby', 
        ("Paraota", "Idly", "Dosa"): 'ojaswi'}

print(d)

Output
{('chapathi', 'roti'): 'Bobby', ('Paraota', 'Idly', 'Dosa'): 'ojaswi'}

Using dict()

This method allows for dynamic creation of dictionaries from lists, tuples, or other iterables. It is particularly useful when working with structured data from databases, CSV files or APIs, as it provides flexibility in building dictionaries programmatically. This approach is highly efficient when handling large datasets .

Python
d = dict([
    ('Bobby', ('chapathi', 'roti')),
    ('Ojaswi', ('Paraota', 'Idly', 'Dosa'))
])

print(d)

Output
{'Bobby': ('chapathi', 'roti'), 'Ojaswi': ('Paraota', 'Idly', 'Dosa')}

Explanation: dict() constructor converts a list of tuples into a dictionary, where each tuple represents a key-value pair.

Using dictionary comprehension

This approach provides a concise way to construct dictionaries of tuples from existing lists or sequences. It is especially useful when performing data transformations, mapping relationships or filtering elements dynamically. Since dictionary comprehension is optimized in Python, this method ensures better performance over traditional loops while keeping the code clean and readable.

Python
a = ['Bobby', 'Ojaswi']  # name
b = [('chapathi', 'roti'), ('Paraota', 'Idly', 'Dosa')]  # food

d = {name: food for name, food in zip(a, b)}

print(d)

Output
{'Bobby': ('chapathi', 'roti'), 'Ojaswi': ('Paraota', 'Idly', 'Dosa')}

Explanation: zip(a, b) pairs elements from the two lists a for names and b for food tuples. Dictionary comprehension then constructs a dictionary where names are keys and food tuples are values.

Using default dict

This method is highly useful in scenarios where missing keys should have default tuple values instead of causing a KeyError. It simplifies dictionary handling in cases where data is being incrementally built or updated, such as aggregating user preferences, handling missing data gracefully or setting up default structures for further updates.

Python
from collections import defaultdict

d = defaultdict(tuple)
d['Bobby'] = ('chapathi', 'roti')
d['Ojaswi'] = ('Paraota', 'Idly', 'Dosa')

print(dict(d))

Output
{'Bobby': ('chapathi', 'roti'), 'Ojaswi': ('Paraota', 'Idly', 'Dosa')}

Explanation: defaultdict(tuple) creates a dictionary with a default tuple value to prevent KeyError for missing keys. Assigning values (d[‘Bobby’] = (‘chapathi’, ‘roti’)) stores names as keys and food tuples as values. dict(d) converts it back to a regular dictionary .



Practice Tags :

Similar Reads