Data structures & formats · reviewed in August 2026
Dictionary
A dictionary is a collection of key-value pairs in Python, where each key is unique and used to look up its associated value in roughly constant time, regardless of how many items it holds. It's written in braces, like {"name": "Ana", "age": 30}.
user = {"name": "Ana", "age": 30}
user["city"] = "Bogotá"
print(user.get("country", "unknown"))Frequently asked questions
What data types can be keys?
Any immutable, hashable type: strings, numbers, and tuples (as long as their elements are also immutable). Lists and other dictionaries can't be keys.
Do dictionaries keep an order?
Yes, since Python 3.7 dictionaries preserve their keys' insertion order as a language guarantee, though they shouldn't be treated as sorted by value.
How do you safely access a key that might not exist?
With dict.get('key', default_value), which returns the default instead of raising a KeyError when the key is missing.