A Python dictionary that returns a list of values for each key. The dictionary will contain information about my favorite books, including the title, author, and year of publication. The key will be the title of the book, and the values will be a list containing the author and the year of publication. favorite_books = {     "The Great Gatsby": ["F. Scott Fitzgerald", 1925],     "To Kill a Mockingbird": ["Harper Lee", 1960],     "1984": ["George Orwell", 1949] } NOTE: You can replace the book details as your need in above code. Next, modify the invert_dict function from Section 11.5 of "Think Python" to invert my dictionary. The modified function will need to turn each of the list items into separate keys in the inverted dictionary. def invert_dict(d):     inverse = dict()     for key in d:         vals = d[key]         for val in vals:             if val not in inverse:                 inverse[val] = [key]             else:                 inverse[val].append(key)     return inverse Here, the function takes a dictionary as an argument and returns an inverted dictionary where the keys are the values of the original dictionary, and the values are a list of keys from the original dictionary that had the corresponding value. Finally, I will run the invert_dict function on my favorite_books dictionary and print the original and inverted dictionaries. print("Original dictionary:") print(favorite_books) inverted_dict = invert_dict(favorite_books) print("Inverted dictionary:") print(inverted_dict) Python Code and Explanation: favorite_books = { "The Great Gatsby": ["F. Scott Fitzgerald", 1925], "To Kill a Mockingbird": ["Harper Lee", 1960], "1984": ["George Orwell", 1949] } def invert_dict(d):     inverse = dict()     for key in d:         vals = d[key]         for val in vals:             if val not in inverse:                 inverse[val] = [key]             else:                 inverse[val].append(key)     return inverse print("Original dictionary:") print(favorite_books) inverted_dict = invert_dict(favorite_books) print("Inverted dictionary:") print(inverted_dict) This program defines a dictionary named favorite_books that stores information about three books: "The Great Gatsby", "To Kill a Mockingbird", and "1984". Each book is associated with a list of two items: the author's name and the year of publication. The program then defines a function invert_dict(d) that takes a dictionary d as input and returns a new dictionary where the keys and values of the input dictionary are inverted. Specifically, each value in the input dictionary becomes a key in the output dictionary, and the corresponding keys in the input dictionary become the values in the output dictionary. If multiple keys in the input dictionary have the same value, the corresponding keys are stored in a list as the value in the output dictionary. The program then applies the invert_dict() function to the favorite_books dictionary and assigns the result to a new variable named inverted_dict. Finally, the program prints the original and inverted dictionaries. The output of the program shows the original and inverted dictionaries. The original dictionary lists information about three books. The inverted dictionary lists the authors and publication years as keys, with the corresponding book titles as the values. Note that since the publication years of "The Great Gatsby" and "1984" are the same, their corresponding book titles are stored as a list under the key "1949". Modify your above program to read dictionary items from a file and write the inverted dictionary to a file.  Include the following in your Learning Journal submission:  The input file for your original dictionary (with at least six items). The Python program to read from a file, invert the dictionary, and write to a different file. The output file for your inverted dictionary. The code and its output must be explained technically. The explanation can be provided before or after the code, or in the form of comments within the code. For code modification type of questions, always mention or clearly highlight the part which is modified, along with the reason stated, as a code comment. The descriptive part of your response must be at least 200 words.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

A Python dictionary that returns a list of values for each key. The dictionary will contain information about my favorite books, including the title, author, and year of publication. The key will be the title of the book, and the values will be a list containing the author and the year of publication.

favorite_books = {
    "The Great Gatsby": ["F. Scott Fitzgerald", 1925],
    "To Kill a Mockingbird": ["Harper Lee", 1960],
    "1984": ["George Orwell", 1949]
}

NOTE: You can replace the book details as your need in above code.

Next, modify the invert_dict function from Section 11.5 of "Think Python" to invert my dictionary. The modified function will need to turn each of the list items into separate keys in the inverted dictionary.

def invert_dict(d):
    inverse = dict()
    for key in d:
        vals = d[key]
        for val in vals:
            if val not in inverse:
                inverse[val] = [key]
            else:
                inverse[val].append(key)
    return inverse

Here, the function takes a dictionary as an argument and returns an inverted dictionary where the keys are the values of the original dictionary, and the values are a list of keys from the original dictionary that had the corresponding value.

Finally, I will run the invert_dict function on my favorite_books dictionary and print the original and inverted dictionaries.

print("Original dictionary:")
print(favorite_books)

inverted_dict = invert_dict(favorite_books)
print("Inverted dictionary:")
print(inverted_dict)

Python Code and Explanation:

favorite_books = {
"The Great Gatsby": ["F. Scott Fitzgerald", 1925],
"To Kill a Mockingbird": ["Harper Lee", 1960],
"1984": ["George Orwell", 1949]
}
def invert_dict(d):
    inverse = dict()
    for key in d:
        vals = d[key]
        for val in vals:
            if val not in inverse:
                inverse[val] = [key]
            else:
                inverse[val].append(key)
    return inverse
print("Original dictionary:")
print(favorite_books)

inverted_dict = invert_dict(favorite_books)
print("Inverted dictionary:")
print(inverted_dict)

This program defines a dictionary named favorite_books that stores information about three books: "The Great Gatsby", "To Kill a Mockingbird", and "1984". Each book is associated with a list of two items: the author's name and the year of publication.

The program then defines a function invert_dict(d) that takes a dictionary d as input and returns a new dictionary where the keys and values of the input dictionary are inverted. Specifically, each value in the input dictionary becomes a key in the output dictionary, and the corresponding keys in the input dictionary become the values in the output dictionary. If multiple keys in the input dictionary have the same value, the corresponding keys are stored in a list as the value in the output dictionary.

The program then applies the invert_dict() function to the favorite_books dictionary and assigns the result to a new variable named inverted_dict. Finally, the program prints the original and inverted dictionaries.

The output of the program shows the original and inverted dictionaries. The original dictionary lists information about three books. The inverted dictionary lists the authors and publication years as keys, with the corresponding book titles as the values. Note that since the publication years of "The Great Gatsby" and "1984" are the same, their corresponding book titles are stored as a list under the key "1949".

Modify your above program to read dictionary items from a file and write the inverted dictionary to a file. 

Include the following in your Learning Journal submission: 

  • The input file for your original dictionary (with at least six items).
  • The Python program to read from a file, invert the dictionary, and write to a different file.
  • The output file for your inverted dictionary.
The code and its output must be explained technically. The explanation can be provided before or after the code, or in the form of comments within the code. For code modification type of questions, always mention or clearly highlight the part which is modified, along with the reason stated, as a code comment. The descriptive part of your response must be at least 200 words.
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education