Question: intersection(d1, d2) (in Python Given two dictionaries d1 and d2, return a new dictionary that contains only the key:value pairs that exist in both d1
intersection(d1, d2) (in Python Given two dictionaries d1 and d2, return a new dictionary that contains only the key:value pairs that exist in both d1 and d2. It should not modify the contents of the provided dictionaries. If no same mapping exists, return an empty dictionary. You cannot use sets. Remember that dictionaries are unsorted collections, the order of the key:value pairs does not matter as long as the keys map to the same value Preconditions d1: dict d2: dict Returns: dict -> key:value pairs that appear in both d1 and d2 Examples: >>> intersection({'a':5, 'b':7, 'd':5},{'d':5, 'a':5, 'b':9, 'c':12}) {'a': 5, 'd': 5} >>> intersection({'a':5, 'b':7, 'd':5},{'d':8, 'a':51, 'b':9, 'c':12}) {} >>> intersection({'a':5, 'b':7, 'd':5, 'c':'32', 't':35.6},{'d':8, 'a':1, 'b':9, 'c':'32'}) {'c': '32'}
def intersection(d1, d2): """ >>> intersection({'a':5, 'b':7, 'd':5},{'d':5, 'a':5, 'b':9, 'c':12}) {'a': 5, 'd': 5} >>> intersection({'a':5, 'b':7, 'd':5},{'d':8, 'a':51, 'b':9, 'c':12}) {} >>> dict_one = {'a':5, 'b':7, 'd':5, 'c':'32', 'art':35.6} >>> dict_two = {'d':8, 'a':51, 'b':9, 'c':'32'} >>> intersection(dict_one, dict_two) {'c': '32'} >>> dict_one {'a': 5, 'b': 7, 'd': 5, 'c': '32', 'art': 35.6} >>> dict_two {'d': 8, 'a': 51, 'b': 9, 'c': '32'} """ # - YOUR CODE STARTS HERE - Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
