logo CodeStepByStep logo

is_sub_dict

Language/Type: Python dict collections

Write a function named is_sub_dict that accepts two dictionaries from strings to strings as its parameters and returns True if every key in the first dictionary is also contained in the second dictionary and maps to the same value in the second dictionary.

For example, given the dictionaries below, map1 is a sub-dict of map2, so the call of is_sub_dict(map1, map2) would return True. The order of the parameters does matter, so the call of is_sub_dict(map2, map1) would return False.

But map3 is not a sub-dictionary of map2 because the key "Alisha" is not in map2 and also because the key "Smith" does not map to the same value as it does in map2 therefore the call of is_sub_dict(map3, map2) would return False. The empty dictionary is considered to be a sub-dictionary of every dictionary, so the call of is_sub_dict(map4, map1) would return True.

map1: {Smith': '949-0504', 'Marty': '206-9024}
map2: {Marty': '206-9024', 'Hawking': '123-4567', 'Smith': '949-0504', 'Newton': '123-4567}
map3: {Alisha': '321-7654', 'Hawking': '123-4567', 'Smith': '888-8888
map4: {}

Constraints: You may not declare any auxiliary data structures in solving this problem.

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.