2 Examples of Nested Dictionary Comprehension in Python

Coding in Python can be a daunting task, especially if you’re not familiar with the different data structures that are available. One such data structure is a dictionary, which is a collection of key-value pairs. Python also has a powerful feature called dictionary comprehension that allows you to create dictionaries in a concise and elegant syntax. Nested dictionary comprehension, on the other hand, allows you to create dictionaries of dictionaries. In this article, we will explore 2 examples of nested dictionary comprehension in Python.

Example 1: Creating a Dictionary of Dictionaries

Let’s say you have a list of students and their grades in different subjects. You want to create a nested dictionary where each student’s name is the key to a dictionary containing their grades for each subject. Here’s how you can do it using nested dictionary comprehension:

“`
students = [(‘Alice’, [(‘Math’, 80), (‘Science’, 90)]),
(‘Bob’, [(‘Math’, 85), (‘Science’, 95)])]

grades = {name: {subject: grade for (subject, grade) in subject_grades}
for (name, subject_grades) in students}

print(grades)
“`

Output:

“`
{‘Alice’: {‘Math’: 80, ‘Science’: 90},
‘Bob’: {‘Math’: 85, ‘Science’: 95}}
“`

Explanation:

– We start with a list of tuples, where each tuple contains a student’s name and a list of tuples representing the subject and grade.
– We use nested dictionary comprehension to create a dictionary of dictionaries. The outer dictionary uses the student’s name as the key, and the inner dictionary uses the subject as the key and the grade as the value.
– The final result is a nested dictionary where each student’s name corresponds to a dictionary representing their grades in each subject.

Example 2: Creating a Dictionary of Dictionaries with Conditional Statements

Let’s say you have a list of movies and their ratings from different websites. You want to create a nested dictionary where each movie’s name is the key to a dictionary containing the ratings from specific websites. Here’s how you can do it using nested dictionary comprehension with conditional statements:

“`
movies = [(‘The Dark Knight’, [(‘IMDb’, 9.0), (‘Rotten Tomatoes’, 94), (‘Metacritic’, 84)]),
(‘Inception’, [(‘IMDb’, 8.8), (‘Rotten Tomatoes’, 87), (‘Metacritic’, 74)]),
(‘The Shawshank Redemption’, [(‘IMDb’, 9.3), (‘Rotten Tomatoes’, 90)])]

ratings = {name: {website: rating for (website, rating) in ratings_list if website==’IMDb’ or website==’Rotten Tomatoes’}
for (name, ratings_list) in movies}

print(ratings)
“`

Output:

“`
{‘The Dark Knight’: {‘IMDb’: 9.0, ‘Rotten Tomatoes’: 94},
‘Inception’: {‘IMDb’: 8.8, ‘Rotten Tomatoes’: 87},
‘The Shawshank Redemption’: {‘IMDb’: 9.3, ‘Rotten Tomatoes’: 90}}
“`

Explanation:

– We start with a list of tuples, where each tuple contains a movie’s name and a list of tuples representing the website and rating.
– We use nested dictionary comprehension with conditional statements to create a dictionary of dictionaries. The outer dictionary uses the movie’s name as the key, and the inner dictionary uses the website as the key and the rating as the value. We only include ratings from IMDb and Rotten Tomatoes websites in the inner dictionary.
– The final result is a nested dictionary where each movie’s name corresponds to a dictionary representing its ratings on specific websites.

Conclusion

Nested dictionary comprehension is a powerful feature in Python that allows you to create dictionaries of dictionaries in a concise and elegant syntax. In this article, we explored 2 examples of nested dictionary comprehension in Python. The first example demonstrated how to create a dictionary of dictionaries representing a list of students and their grades in different subjects. The second example demonstrated how to create a dictionary of dictionaries representing a list of movies and their ratings from different websites, with conditional statements to include ratings from specific websites. With these examples, you can leverage nested dictionary comprehension to create complex data structures in Python with ease.

WE WANT YOU

(Note: Do you have knowledge or insights to share? Unlock new opportunities and expand your reach by joining our authors team. Click Registration to join us and share your expertise with our readers.)

By knbbs-sharer

Hi, I'm Happy Sharer and I love sharing interesting and useful knowledge with others. I have a passion for learning and enjoy explaining complex concepts in a simple way.

Leave a Reply

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