Nested Collections

Collections hold other collections. A list of lists is a grid. A dictionary of lists groups things by category. Reading them is the same skill as always: one layer at a time.

EXAMPLE
loading editor…
YOUR TURN
Your turn → grid[1][2] reads left to right: row 1, then column 2. Print the value 4.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Grouping is the pattern that turns a flat list into an organized one, and it appears in real software constantly.

EXAMPLE
loading editor…
YOUR TURN
Your turn → The check 'if this key does not exist yet, start an empty list' is the heart of grouping. Add two more businesses and rerun.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

◈ Graded Challenge

Write a function that takes a list of member records (each with a name and hours) and returns a list of just the names, sorted from most hours to fewest. The original list must not be modified.

Name it exactly: def top_volunteers(members):

3 visible + 2 hidden
loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back