Daily Python 3 – Nested dictionaries

in the following excersise we have a dictionary inside a dictionary (nested dictionaries) and we want to add information to the nested dictionaries. Namely, the rating a book received out of 10. You have a list corresponding to the books in order. Use a for loop to add the new information to the nested dictionaries.

This is doable in 2 lines. Tips: use enumerate and f-strings!

Copy the following:

rating = [9,8,4]

book_dict = {
    'book1': {'name': 'Little women', 'pages': 759},
    'book2': {'name': 'War and Peace', 'pages': 1225},
    'book3': {'name': 'The Prince', 'pages': 164}
}

an example of how to access data in a nested dictionary is:

book_dict['book1']['pages']
>>> 759

Leave a Reply

Your email address will not be published.