Learn Python
>>> data_structures.dictionaries

Dictionaries in Python

#concept

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.

#how it works
1

Create with curly braces

{key1: value1, key2: value2, ...} pairs each key with a value.

2

Access by key

d["key"] looks up a value — raises a KeyError if the key doesn’t exist.

3

Look up safely with .get()

.get("key", default) returns a fallback instead of erroring if the key is missing.

4

Assigning adds or updates

A new key is added; an existing key is updated — the same syntax handles both.

5

Loop with .keys(), .values(), .items()

Gives access to a dictionary’s keys, values, or key-value pairs for iteration.

#example
person = {"name": "Ada", "age": 28}
print(person["name"])
print(person.get("email", "not provided"))
 
person["age"] = 29
person["city"] = "London"
print(person)
Output
Ada
not provided
{'name': 'Ada', 'age': 29, 'city': 'London'}
1person["name"] looks up the value for the key "name".
2person.get("email", "not provided") looks up a missing key safely, returning the fallback instead of raising an error.
3person["age"] = 29 updates the existing key’s value.
4person["city"] = "London" adds a brand new key-value pair, since "city" didn’t exist yet.
#use cases
Looking up a value by a meaningful name, like a config setting or a translation table
Counting occurrences of items, using each item as a key and a running count as the value
Representing structured records, similar to a small JSON object
Fast lookups by key, which stay close to instant regardless of how many items the dictionary holds

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.