Functions & Data · 9 min read · Beginner
Lists & Dictionaries
Store ordered collections in lists, key-value records in dicts, and look up missing keys safely with .get().
What you'll learn
- Slice, append, and iterate over Python lists
- Store structured data in dictionaries and iterate with .items()
- Avoid KeyError crashes by using dict.get(key, default)
Lists ([1, 2, 3]) hold ordered sequences, while Dictionaries ({"track": "Python", "free": True}) store fast key-value mappings.
Safe Dictionary Lookups with .get()
Accessing course["price"] raises a KeyError if "price" does not exist. Calling course.get("price", 0) safely returns 0 instead!
Fill-in-the-Blank Exercise
List Append & Safe Dict Lookup
Add 'Python' to tracks and safely read the 'level' key with a default of 'Beginner'.
tracks = ["HTML", "CSS"]
tracks.("Python")
course = {"title": "Learn Python"}
level = course.("level", "Beginner")Try It Yourself
PythonLists, Slicing & Dictionaries
Python Output (WebAssembly / Pyodide)
Click Run Python to execute real Python 3 right in your browser.
Check Your Understanding
Score: 0 / 1
1. What does `{'a': 1}.get('b', 99)` return?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.