Dictionaries in Python
A dictionary stores key-value pairs, letting you look up a value by a meaningful key instead of a numeric position. Dictionaries are written with curly braces and colons, and since Python 3.7, they reliably preserve the order items were added in.
Create with curly braces
{key1: value1, key2: value2, ...} pairs each key with a value.
Access by key
d["key"] looks up a value — raises a KeyError if the key doesn’t exist.
Look up safely with .get()
.get("key", default) returns a fallback instead of erroring if the key is missing.
Assigning adds or updates
A new key is added; an existing key is updated — the same syntax handles both.
Loop with .keys(), .values(), .items()
Gives access to a dictionary’s keys, values, or key-value pairs for iteration.
person = {"name": "Ada", "age": 28}print(person["name"])print(person.get("email", "not provided")) person["age"] = 29person["city"] = "London"print(person)Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.