Skip to content
dreamcode
dreamcode
Map
Dictionaries
Lesson 28 of 77
+15 XP on finish
PYTHON BASICSChapter 5 · Collections

Labeling values

A dictionary stores values mapped to keys (labels). Define it with curly braces {} and key-value pairs separated by colons. Retrieve values using their keys.

Worked example

How it reads

  • {"name": "Sirius"} maps the key "name" to the value "Sirius".
  • star["name"] retrieves the value stored under the key "name".
Cloud tip: If you try to look up a key that doesn't exist, Python will raise a KeyError. Check spelling.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Add a "height" key with the value 3000, then print the whole dictionary: {'name': 'cloud', 'color': 'neon', 'height': 3000}.

Press Run to check your work.