Python Programming - Tricky Questions - Discussion

Discussion Forum : Tricky Questions - General Questions (Q.No. 4)
4.
What is the correct way to check if a key is present in a dictionary?
my_dict = {'name': 'John', 'age': 25}
key_to_check = 'age'
if key_to_check in my_dict.keys():
if key_to_check in my_dict:
if my_dict.contains(key_to_check):
if my_dict.exists(key_to_check):
Answer: Option
Explanation:
The correct way to check if a key is present in a dictionary is to use the in keyword directly on the dictionary.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.