How to Access Values in a Dictionary in Python
A Python dictionary stores data as key-value pairs. To get a value, you use its key.
This page shows the most common ways to access dictionary values:
- Direct access with square brackets
- Safer access with
get() - Checking whether a key exists
- Accessing nested dictionary values
- Looping through all key-value pairs
Quick answer
person = {"name": "Ana", "age": 25}
print(person["name"]) # Ana
print(person.get("age")) # 25
print(person.get("city")) # None
Use square brackets when the key must exist. Use get() when the key might be missing.
What this page helps you do
- Access a value by its key
- Use a safer method when a key may not exist
- Understand the difference between
[]andget() - Avoid common dictionary access errors
Access a value with square brackets
Use dictionary_name[key] to get a value from a dictionary.
person = {"name": "Ana", "age": 25}
print(person["name"])
print(person["age"])
Output:
Ana
25
This is the most direct way to access a value.
Use this when:
- You know the key exists
- The key is required for your program to work
If the key does not exist, Python raises a KeyError.
person = {"name": "Ana", "age": 25}
print(person["city"])
This causes an error because "city" is not a key in the dictionary.
If you want to learn more about this problem, see how to fix KeyError when accessing dictionary values.
Access a value with get()
Use dictionary_name.get(key) when the key may be missing.
person = {"name": "Ana", "age": 25}
print(person.get("name"))
print(person.get("city"))
Output:
Ana
None
If the key is not found, get() returns None instead of raising an error.
You can also provide a default value:
person = {"name": "Ana", "age": 25}
print(person.get("city", "Unknown"))
Output:
Unknown
This is useful when:
- Data is optional
- You are working with user input
- You are reading data from an API or file
- Missing keys are normal
If you want a full method reference, see Python dictionary get() method.
Check if a key exists before accessing it
Use key in dictionary_name to check whether a key exists.
person = {"name": "Ana", "age": 25}
if "name" in person:
print(person["name"])
if "city" in person:
print(person["city"])
else:
print("city was not found")
Output:
Ana
city was not found
This helps you avoid KeyError and lets you run different code depending on whether the key is present.
Use this approach when:
- You need different logic for missing keys
- You want to show a custom message
- You only want to access the value if the key exists
For a full beginner guide, see how to check if a key exists in a dictionary in Python.
Access nested dictionary values
Sometimes a dictionary value is another dictionary.
data = {
"user": {
"name": "Ana",
"age": 25
}
}
print(data["user"]["name"])
Output:
Ana
This works when all keys exist.
If nested keys may be missing, get() is safer:
data = {
"user": {
"name": "Ana"
}
}
user = data.get("user", {})
print(user.get("name"))
print(user.get("age", "Not provided"))
Output:
Ana
Not provided
Breaking nested access into steps often makes your code easier to read.
Loop through a dictionary to access many values
If you want to access all entries, loop through the dictionary.
Use .items() to get both the key and the value:
person = {"name": "Ana", "age": 25, "city": "Madrid"}
for key, value in person.items():
print(key, "->", value)
Output:
name -> Ana
age -> 25
city -> Madrid
This is useful when you want to:
- Print all data
- Process each key-value pair
- Build reports or summaries
To learn more, see Python dictionary items() method.
When to use each approach
Use the method that matches your situation:
- Use
[]when the key must exist - Use
get()when missing keys are normal - Use
inwhen you need to branch based on whether a key exists - Use
.items()when you need all key-value pairs
A simple rule:
- Required key → use
[] - Optional key → use
get()
Common mistakes
These are common reasons dictionary access fails:
- Using a key that is not in the dictionary
- Using the wrong spelling or capitalization for a key
- Assuming nested keys always exist
- Confusing dictionary keys with list indexes
- Trying to access values before the dictionary is fully built
These quick checks can help when debugging:
print(my_dict)
print(my_dict.keys())
print("name" in my_dict)
print(my_dict.get("name"))
print(type(my_dict))
What these do:
print(my_dict)shows the full dictionaryprint(my_dict.keys())shows all available keysprint("name" in my_dict)checks whether"name"existsprint(my_dict.get("name"))safely tries to get the valueprint(type(my_dict))confirms that the object is really a dictionary
If you are new to dictionaries, see Python dictionaries explained.
FAQ
What is the safest way to access a dictionary value?
Use get() if the key may be missing. It avoids a KeyError.
What happens if I use square brackets with a missing key?
Python raises a KeyError.
How do I return a default value if a key is missing?
Use my_dict.get("key", default_value).
How do I get all values from a dictionary?
Use my_dict.values() to view all values. You can also read how to get all values from a dictionary in Python.
How do I access a nested dictionary value?
Use multiple keys, such as data["user"]["name"], or safer chained get() calls.