Python Program - Check if a Key is Already Present in a Dictionary
To check if a key is already present in a dictionary in Python, you can use the in keyword. Here's an example program:
my_dict = {"apple": 1, "banana": 2, "cherry": 3}
if "banana" in my_dict:
print("The key 'banana' is present in the dictionary")
else:
print("The key 'banana' is not present in the dictionary")
In this program, the in keyword is used to check if the key "banana" is present in the my_dict dictionary. If the key is present, the program will display a message saying that the key is present. If the key is not present, the program will display a message saying that the key is not present.
When you run this program, the output will be:
The key 'banana' is present in the dictionary
