Checking Key Existence in Dictionary

Write a Python function that checks if a given key exists in a dictionary. If the key exists, return True, otherwise, return False.

Example 1:

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

Example 2:

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

Use the ‘in’ keyword to check if a key exists in a dictionary.

def check_key(dict, key):
return key in dict

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!