A list ["Ada", "Bob", "Cleo"] orders three names but doesn't tell you anything about each name — say their ages. You could keep two parallel lists, but they'd have to stay in sync.
Risky — easy to add a name and forget to add the matching age.
A dictionary pairs each value with a name (a key). Curly braces, key: value pairs, comma-separated:
ages = {"Ada": 36, "Bob": 41, "Cleo": 28}
print(ages["Ada"]) # 36Keys are usually strings; values can be anything.
Square brackets — same as list indexing?
Same syntax, different meaning. With a list, the brackets hold an integer index. With a dict, they hold a key. Python dispatches based on the type of the container.
What if I look up a key that's not there?
KeyError. The program crashes, just like an out-of-range list index. The safer alternative is .get(key) which returns None for missing keys, or .get(key, default) for a fallback value: ages.get("Zed", 0) returns 0 if Zed isn't in the dict.
Can I add a new key after creation?
Just assign: ages["Dan"] = 22 adds a new pair. If the key already exists, it overwrites the old value. Same = you've been using all along.
{key: value, ...}A dictionary stores named values. Curly braces, colon between key and value:
ages = {"Ada": 36, "Bob": 41}
capitals = {"France": "Paris", "Japan": "Tokyo"}
empty = {}Keys are usually strings or numbers — must be immutable types. Values can be anything: numbers, strings, lists, even other dicts.
[key]ages["Ada"] # 36
ages["Zed"] # KeyError: 'Zed'Missing key = error. Use .get(key) for a safe lookup:
ages.get("Ada") # 36
ages.get("Zed") # None — no error
ages.get("Zed", 0) # 0 — with default=ages["Dan"] = 22 # adds a new pair
ages["Ada"] = 37 # overwrites the existing valuein checks for keys, not valuesprint("Ada" in ages) # True — `Ada` is a key
print(36 in ages) # False — 36 is a value, but `in` only sees keysThis is different from lists, where in tests element membership. On dicts, in always asks about keys. To check values: 36 in ages.values().
Since Python 3.7, iterating a dict yields keys in the order they were inserted. (Older Python versions had unordered dicts; you can ignore that history.) For most purposes, treat dicts as ordered.
Use a list when order is the meaningful axis (rankings, queues, sequences). Use a dict when labelled lookup is the meaningful axis ("give me the value for key X"). The shape of your access pattern picks the container.
A list ["Ada", "Bob", "Cleo"] orders three names but doesn't tell you anything about each name — say their ages. You could keep two parallel lists, but they'd have to stay in sync.
Risky — easy to add a name and forget to add the matching age.
A dictionary pairs each value with a name (a key). Curly braces, key: value pairs, comma-separated:
ages = {"Ada": 36, "Bob": 41, "Cleo": 28}
print(ages["Ada"]) # 36Keys are usually strings; values can be anything.
Square brackets — same as list indexing?
Same syntax, different meaning. With a list, the brackets hold an integer index. With a dict, they hold a key. Python dispatches based on the type of the container.
What if I look up a key that's not there?
KeyError. The program crashes, just like an out-of-range list index. The safer alternative is .get(key) which returns None for missing keys, or .get(key, default) for a fallback value: ages.get("Zed", 0) returns 0 if Zed isn't in the dict.
Can I add a new key after creation?
Just assign: ages["Dan"] = 22 adds a new pair. If the key already exists, it overwrites the old value. Same = you've been using all along.
{key: value, ...}A dictionary stores named values. Curly braces, colon between key and value:
ages = {"Ada": 36, "Bob": 41}
capitals = {"France": "Paris", "Japan": "Tokyo"}
empty = {}Keys are usually strings or numbers — must be immutable types. Values can be anything: numbers, strings, lists, even other dicts.
[key]ages["Ada"] # 36
ages["Zed"] # KeyError: 'Zed'Missing key = error. Use .get(key) for a safe lookup:
ages.get("Ada") # 36
ages.get("Zed") # None — no error
ages.get("Zed", 0) # 0 — with default=ages["Dan"] = 22 # adds a new pair
ages["Ada"] = 37 # overwrites the existing valuein checks for keys, not valuesprint("Ada" in ages) # True — `Ada` is a key
print(36 in ages) # False — 36 is a value, but `in` only sees keysThis is different from lists, where in tests element membership. On dicts, in always asks about keys. To check values: 36 in ages.values().
Since Python 3.7, iterating a dict yields keys in the order they were inserted. (Older Python versions had unordered dicts; you can ignore that history.) For most purposes, treat dicts as ordered.
Use a list when order is the meaningful axis (rankings, queues, sequences). Use a dict when labelled lookup is the meaningful axis ("give me the value for key X"). The shape of your access pattern picks the container.
Create a free account to get started. Paid plans unlock all tracks.