Checking Multiple Keys in Dictionary

Write a Python function that takes a dictionary and a list of keys as parameters. The function should return True if all keys in the list are present in the dictionary, otherwise, return False.

Example 1:

Input: {"a": 1, "b": 2, "c": 3}, ["a", "b"] 
Output: True

Example 2:

Input: {"a": 1, "b": 2, "c": 3}, ["a", "d"] 
Output: False

Use Python’s all function and list comprehension to check for multiple keys in the dictionary.

def check_keys(dict, keys):
return all(key in dict for key in keys)

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!