Write a function multiply_values (d1, d2) that takes in two dictionaries. In both dictionaries, the keys are strings, and the values are integers. The function should return a new dictionary which contains only the keys present in both dictionaries. The value for each key should be the product of the values for that key in the two input dictionaries. This function should not alter either of the two dictionaries passed in as arguments. Examples: >>> multiply_values ({'e':5, 'h':8, 'm':3, 'o':5}, {'e':1, 'o':4, 'r':10}) {'e': 5, 'o': 20} >>> multiply_values ({'x':12, 'y':43}, {}) {} >>> multiply_values({'a':11, 'c':10, 'n':2}, {'a':1, 'c':2, 'o':3, 'n':4}) {'n': 8, 'c': 20, 'a': 11}

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
Question

In python answer wuick

**Title: Function to Multiply Dictionary Values**

**Description:**

This lesson will guide you through writing a Python function `multiply_values(d1, d2)`, designed to take in two dictionaries. In both dictionaries, the keys are strings, and the values are integers.

**Function Objective:**

The function should return a new dictionary containing only the keys present in both dictionaries. For each key, the value should be the product of the values for that key in the two input dictionaries.

**Key Requirements:**

- The function must not modify either of the input dictionaries.

**Examples:**

1. **Example 1:**
   ```python
   multiply_values({'e': 5, 'h': 8, 'm': 3, 'o': 5}, {'e': 1, 'o': 4, 'r': 10})
   ```
   **Output:**
   ```python
   {'e': 5, 'o': 20}
   ```
   Explanation: The keys `'e'` and `'o'` are present in both dictionaries. Their multiplied values are `5` and `20`, respectively.

2. **Example 2:**
   ```python
   multiply_values({'x': 12, 'y': 43}, {})
   ```
   **Output:**
   ```python
   {}
   ```
   Explanation: No common keys exist between the dictionaries, resulting in an empty dictionary.

3. **Example 3:**
   ```python
   multiply_values({'a': 11, 'c': 10, 'n': 2}, {'a': 1, 'c': 2, 'o': 3, 'n': 4})
   ```
   **Output:**
   ```python
   {'a': 11, 'c': 20, 'n': 8}
   ```
   Explanation: The keys `'a'`, `'c'`, and `'n'` are present in both dictionaries. Their resulting products are `11`, `20`, and `8`, respectively.

Utilize this function to efficiently process and manipulate dictionary data by focusing on common keys and calculating their product.
Transcribed Image Text:**Title: Function to Multiply Dictionary Values** **Description:** This lesson will guide you through writing a Python function `multiply_values(d1, d2)`, designed to take in two dictionaries. In both dictionaries, the keys are strings, and the values are integers. **Function Objective:** The function should return a new dictionary containing only the keys present in both dictionaries. For each key, the value should be the product of the values for that key in the two input dictionaries. **Key Requirements:** - The function must not modify either of the input dictionaries. **Examples:** 1. **Example 1:** ```python multiply_values({'e': 5, 'h': 8, 'm': 3, 'o': 5}, {'e': 1, 'o': 4, 'r': 10}) ``` **Output:** ```python {'e': 5, 'o': 20} ``` Explanation: The keys `'e'` and `'o'` are present in both dictionaries. Their multiplied values are `5` and `20`, respectively. 2. **Example 2:** ```python multiply_values({'x': 12, 'y': 43}, {}) ``` **Output:** ```python {} ``` Explanation: No common keys exist between the dictionaries, resulting in an empty dictionary. 3. **Example 3:** ```python multiply_values({'a': 11, 'c': 10, 'n': 2}, {'a': 1, 'c': 2, 'o': 3, 'n': 4}) ``` **Output:** ```python {'a': 11, 'c': 20, 'n': 8} ``` Explanation: The keys `'a'`, `'c'`, and `'n'` are present in both dictionaries. Their resulting products are `11`, `20`, and `8`, respectively. Utilize this function to efficiently process and manipulate dictionary data by focusing on common keys and calculating their product.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Lists
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.
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