23.1k views
5 votes
find the inverse, 4 int = ("pasta" -1 , "potate" 2 , Trice" 3}. (b) [3] Assume that di and d2 are two dictionaries which contain mene bogs in commoni. Write a rode (i.e, do not use ready-made commands) to carate the dictionary da which conetains the items in d I whose loy does not sppear as a kiy in d2. Foe examgle, if d1 - 1l 4cat − , 2. "dog", 3: "bird" } and d2={2 : goot", 4: -botse" }, then d3={1 : feat", 3 − -bird" }.

User Eric Bock
by
7.4k points

1 Answer

4 votes

Final answer:

To create a new dictionary that includes only the key-value pairs from the first dictionary whose keys are not present in the second dictionary, iterate through the keys of the first dictionary, checking if they are in the second, and if not, add them to a new dictionary.

Step-by-step explanation:

The solution to creating a dictionary d3 containing items from dictionary d1 whose keys do not appear in dictionary d2 involves writing a piece of code that iterates through the first dictionary and checks each key against the keys of the second dictionary. If a key from d1 is not found in d2, the key-value pair is added to the new dictionary d3.

Here is an example of how this can be achieved in Python:

Python code:

\\d1 = {1: "cat", 2: "dog", 3: "bird"}\\d2 = {2: "goat", 4: "horse"}\\d3 = {}\\for key in d1:\\ if key not in d2:\\ d3[key] = d1[key]\\

This code will result in d3 having the following key-value pairs: {1: "cat", 3: "bird"}, as these are the keys from d1 that are not present in d2.

User David Boho
by
7.7k points