Store and process collections
Use lists and dictionaries to organize related data.
- Index a list, loop through values and look up dictionary entries.
A list stores an ordered, changeable sequence. Indexes start at zero. A dictionary stores values under keys, making it useful for named fields and lookups. Both can hold values of different types.
1scores = [82, 95, 88]
2print(scores[0])
3student = {"name": "Ada"}
4print(student["name"])82 Ada
Use append to add an item to a list and len to count items. Loop over a list to process every value. Dictionary keys are commonly strings, and a missing key can raise KeyError.
Key takeaways
Index a list, loop through values and look up dictionary entries.
Read the output to check what your program did.
Keep indentation consistent in Python blocks.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Average a list of scores
Read a line of space-separated integers, then print their average. The test inputs contain at least one score.
- Three scores
- Two scores
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…