← Lessons/Lists, Dicts & Comprehensions
← Lists, Dicts & Comprehensions

A dictionary

Learn

A dict: look things up by name

A dict (dictionary) maps keys to values. Instead of "give me item number 2", you say "give me the value filed under this key", like a coat-check ticket:

owners = {"billing": "ana", "search": "ben"}
print(owners["search"])     # ben — look up by key

To walk every pair, use .items(), which hands you the key and the value together:

for team, person in owners.items():
    print(team, person)

Lists are for order and sequence; dicts are for labelled lookup. The editor opens with your script from the previous step.

Make a dict replicas = {"web": 3, "api": 2}. Print web: 3 by looking up the key, then loop .items() printing <name> -> <n> for each.

$ python3 main.py
Loading...

Output

Run your solution to see its output here.