Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Organise the chaos
EXPERIMENT 12 / 101

Give your data a map.

Replace mysterious positions with clear keys and useful values.

BY NAVAN SAMI◷ 6 MIN READPYTHON 3

01 The idea

A dictionary maps unique keys to values. Instead of remembering which position contains a project’s status, ask for the "status" key. Dictionaries preserve insertion order. Keys must be hashable; strings and integers are common choices.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

dictionaries.py
project = {"name": "Orbit", "status": "building"}
project["tasks"] = 7

print(project["name"])
print(project.get("owner", "Unassigned"))
for key, value in project.items():
    print(f"{key}: {value}")
↳ EXPECTED OUTPUT
Orbit
Unassigned
name: Orbit
status: building
tasks: 7

03 Under the hood

  1. 1

    Square brackets retrieve the value for an existing key. Assigning through brackets inserts or replaces a value.

  2. 2

    get() returns a fallback when a key is absent. It does not insert the fallback into the dictionary.

  3. 3

    items() lets you loop over key/value pairs. Use descriptive, consistent keys instead of relying on a dictionary’s position.

04 Your turn, builder

A SMALL MISSION

Create a profile with name and language keys. Update language to "Python" and print both values.

Reveal one possible solution +
one_possible_solution.py
profile = {"name": "Navan", "language": "JavaScript"}
profile["language"] = "Python"
print(profile["name"], profile["language"])

05 Check your signal

QUICK SIGNAL CHECK

What does data.get("missing", 0) return if the key is absent?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.