1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
natulia [17]
4 years ago
5

Write a Python function merge_dict that takes two dictionaries(d1, d2) as parameters, and returns a modified dictionary(d1) cons

tructed using the following rules. The rules for addition into the new dictionary are as follows: Add key/value pairs from d2 into d1. If a key from d2 already appears in d1: the new value in d1 is the sum of the values If a key appear
Computers and Technology
1 answer:
alexandr402 [8]4 years ago
5 0

Answer:

  1. def merge_dict(d1, d2):
  2.    for key in d2:
  3.        if key in d1:
  4.            d1[key] += d2[key]
  5.        else:
  6.            d1[key] = d2[key]
  7.    
  8.    return d1
  9. d1 = {
  10.    'a': 5,
  11.    'b': 8,
  12.    'c': 9  
  13. }
  14. d2 = {
  15.    'c': 5,
  16.    'd': 10,
  17.    'e': 12
  18. }
  19. modified_dict = merge_dict(d1, d2)
  20. print(modified_dict)

Explanation:

Firstly, create a function <em>merge_dict()</em> with two parameters,<em> d1, d2</em> as required by the question (Line 1)

Since our aim is to join the <em>d2</em> into<em> d1</em>, we should traverse the key of <em>d2</em>. To do so, use a for loop to traverse the <em>d2</em> key (Line 2).

While traversing the d2 key, we need to handle two possible cases, 1) d2 key appears in d1, 2) d2 key doesn't appears in d1. Therefore, we create if and else statement (Line 3 & 5).

To handle the first case, we add the values of d2 key into d1 (Line 4). For second case, we add the d2 key as the new key to d1 (Line 6).

Next, we can create two sample dictionaries (Line 11 - 20) and invoke the function to examine the output. We shall get the output as follows:

{'a': 5, 'b': 8, 'c': 14, 'd': 10, 'e': 12}

You might be interested in
How would you identify communication equipment?
eimsori [14]

Answer

Explanation:

There should be more context to this question but I would say there is a variety of things that could be seen as communication equipment, such as email, telephone, voice message, through mail.

6 0
3 years ago
What is the difference between (IF instructions &amp; WHILE instructions )<br> 0<br> 를 들<br> T<br> !
lara31 [8.8K]

Answer:

While statements determine whether a statement is true or false. If what’s stated is true, then the program runs the statement and returns to the first step. If what’s stated is false, the program exits the while and goes to the next statement. An added step to while statements is turning them into continuous loops. If you don’t change the value so that the condition is never false, the while statement becomes an infinite loop.

If statements are the simplest form of conditional statements, statements that allow us to check conditions and change behavior/output accordingly. The part of the statement following the if is called the condition. If the condition is true, the instruction in the statement runs. If the condition is not true, it does not. The if statements are also compound statements. They have a header (if x) followed by an indented statement (an instruction to be followed is x is true). There is no limit to the number of these indented statements, but there must be at least one.

7 0
3 years ago
Which file types have .exe and .png as their extensions?
cricket20 [7]
EXE files are executable files.
PNG files are picture files like JPEG etc.
7 0
4 years ago
Bunco is a dice throwing game that requires no decisions to be made or skill on the part of the player just luck. In the simples
anygoal [31]
Abcdefghijklmnopqrstuvwxyz now I know my abcs, next time won’t you sing with me :)
8 0
2 years ago
Jasper, a businessperson, has no connection with Yale University, but he has a new line of computer software that he would like
zheka24 [161]

Answer:

The answer is letter D.

Explanation:

It is unlikely that Jasper will be able to register the name "Yale Software," because it falsely suggests a connection to an institution.

3 0
4 years ago
Other questions:
  • You are the IT security administrator for a small corporate network. Samuel Garcia (sgarcia) has been away on vacation and has f
    9·1 answer
  • What does a closed-envelope icon next to an e-mail message header indicate?\?
    8·1 answer
  • If you decide to get married, a trade-off would be that you have to give up being single.
    12·2 answers
  • The first thing you must consider in any type of communication is ______.
    9·2 answers
  • A __________ is when the sender cryptographically marks a message with its private key, which is achieved by a cryptographic alg
    12·1 answer
  • Would a hurricane form if the humidity is really high and the breeze is very low
    11·1 answer
  • WILL GIVE BRAINLIEST
    5·2 answers
  • You are the network administrator for a small company that implements NAT to access the internet. However, you recently acquired
    13·1 answer
  • Which of the following screen elements is a horizontal bar that displays at the
    8·1 answer
  • Before a computer can use a router, what configuration information must it be provided?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!