QUESTION #2 [15 pts]: Write the Python code to implement the…
QUESTION #2 [15 pts]: Write the Python code to implement the function group_by_genre that takes as input a dictionary of books, where the key is a string that represents a book name, and the value is a dictionary with information about the book as shown in the example (spaced for readability). This function creates a new dictionary with keys corresponding to genres, and values are a collection of dictionaries stored in a Python list (see example). In the new dictionary, each book should be represented as a dictionary with keys of book_title, publication_date, and author. You may assume that no books have a title that is the name of a genre. Note that the genre should be removed, and the book title should be added. You are not allowed to use 2D lists as a replacement for a dictionary You might mutate the original dictionary (it is permitted), but it is not required >>> books = { … ‘Harry Potter and the Goblet of Fire’: { … ‘author’: ‘J.K. Rowling’, … ‘publication_date’: 2000, … ‘genre’: ‘fantasy’ … }, … ‘The Hobbit’: { … ‘author’: ‘J.R.R. Tolkien’, … ‘publication_date’: 1937, … ‘genre’: ‘fantasy’ … }, … ‘Pride and Prejudice’: { … ‘author’: ‘Jane Austen’, … ‘publication_date’: 1813, … ‘genre’: ‘romance’ … } … } >>> grouped = group_by_genre(books) >>> grouped { ‘fantasy’: [ { ‘book_title’: ‘Harry Potter and the Goblet of Fire’, ‘publication_date’: 1997, ‘author’: ‘J.K. Rowling’ }, { ‘book_title’: ‘The Hobbit’, ‘publication_date’: 2000, ‘author’: ‘J.R.R. Tolkien’ } ], ‘romance’: [ { ‘book_title’: ‘Pride and Prejudice’, ‘publication_date’: 1813, ‘author’: ‘Jane Austen’ } ] }
Read Details