139k views
5 votes
Given dictionaries, d1 and d2, create a new dictionary with the following property: for each entry (a, b) in d1, if a is not a key of d2 (i.e., not a in d2) then add (a,b) to the new dictionary for each entry (a, b) in d2, if a is not a key of d1 (i.e., not a in d1) then add (a,b) to the new dictionary For example, if d1 is {2:3, 8:19, 6:4, 5:12} and d2 is {2:5, 4:3, 3:9}, then the new dictionary should be {8:19, 6:4, 5:12, 4:3, 3:9} Associate the new dictionary with the variable d3

User Richel
by
5.0k points

1 Answer

6 votes

Answer:

Check the explanation

Step-by-step explanation:

# python code

import sys

import readline

from sys import stdin

import random

d1 = {2:3, 8:19, 6:4, 5:12}

d2 = {2:5, 4:3, 3:9}

d3 = {}

#for each entry (a, b) in d1

for i,v in d1.items():

# if a is not a key of d2

if i not in d2.keys():

# add (a,b) to the new dictionary

d3[i] = v

# for each entry (a, b) in d2

for i,v in d2.items():

# if a is not a key of d1

if i not in d1.keys():

#add (a,b) to the new dictionary

d3[i] = v

print "d3: ", d3

#output: d3: {8: 19, 3: 9, 4: 3, 5: 12, 6: 4}

Kindly check the Screenshot below.

Given dictionaries, d1 and d2, create a new dictionary with the following property-example-1
User Bruckwald
by
4.6k points