|
| Home |
|---|
When working with Python dictionaries, it is very common to check whether a specific key exists before accessing it.
A dictionary stores data in the form of key-value pairs. Each key uniquely identifies a value.
If a program tries to access a key that does not exist, Python raises a KeyError.
To prevent such errors and write safer programs, Python provides several ways to check if a key exists inside a dictionary.
in OperatorThe easiest and most common way to check if a key exists in a dictionary is by using the in operator.
The in operator checks whether the specified key
is present among the dictionary keys.
It returns True if the key exists and
False if the key does not exist.
key in dictionary
This syntax evaluates a boolean condition that can be used inside if statements or loops.
student = {
"name":"Alice",
"age":21
}
if "name" in student:
print("Key exists")
Key exists
The program checks whether the key "name" is present in the dictionary.
Since the dictionary contains the key "name",
the condition becomes True and the message is printed.
If the key does not exist in the dictionary,
the condition evaluates to False.
student = {
"name":"Alice",
"age":21
}
if "course" in student:
print("Key exists")
else:
print("Key does not exist")
Key does not exist
The dictionary does not contain the key "course".
Therefore the condition becomes false and the program executes the else block.
keys() Method
Another way to check whether a key exists
is by using the dictionary keys() method.
The keys() method returns all dictionary keys
as a special object called a dictionary view.
We can then check whether a specific key is present in this collection of keys.
key in dictionary.keys()
student = {
"name":"Alice",
"age":21
}
if "age" in student.keys():
print("Age key exists")
Age key exists
The keys() method returns all dictionary keys.
Python then checks whether the key "age" is present in that list of keys.
Since the key exists, the program prints the message.
get() Method
The dictionary get() method can also be used
to safely access values without generating an error.
If the key exists, the method returns the corresponding value.
If the key does not exist, the method returns None.
student = {
"name":"Alice",
"age":21
}
value = student.get("name")
if value:
print("Key exists")
Key exists
The method retrieves the value associated with the key "name".
Since the key exists, the value "Alice" is returned.
Because the returned value is not None,
the condition becomes true and the message is printed.
Sometimes the key to check may come from user input or another variable in the program.
student = {
"name":"Alice",
"age":21
}
key = "age"
if key in student:
print("Key found")
Key found
The variable key stores the value "age".
The program checks whether this key exists inside the dictionary.
Since the key is present, the condition becomes true and the message is printed.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net