logo CodeStepByStep logo

intersect

Language/Type: Python Collections Maps

Write a function named intersect that accepts two dicts of strings to integers as parameters and that returns a new dictionary whose contents are the intersection of the two. The intersection of two dictionaries is defined here as the set of keys and values that exist in both dictionaries. So if some value K maps to value V in both the first and second dictionary, include it in your result. If K does not exist as a key in both dictionaries, or if K does not map to the same value V in both dictionaries, exclude that pair from your result. For example, consider the following two dictionaries:

{'Janet': 87, 'Logan': 62, 'Whitaker': 46, 'Alyssa': 100, 'Stefanie': 80, 'Jeff': 88, 'Kim': 52, 'Sylvia': 95},
{'Logan': 62, 'Kim': 52, 'Whitaker': 52, 'Jeff': 88, 'Stefanie': 80, 'Brian': 60, 'Lisa': 83, 'Sylvia': 87}

Calling your function on the preceding dictionaries would return the following new dictionary:

{Logan=62, Stefanie=80, Jeff=88, Kim=52

Do not modify the dictionaries passed in as parameters.

Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.