Python (Fall 2026) Module 11-1: Iteration and Collections
Watch on YouTube →
Overview
Bill Moseley reviews Python iteration techniques that prepare students for later coursework: looping through lists, accessing dictionary values through keys, and unpacking nested collections with multiple assignment. He also demonstrates `enumerate()` for pairing items with counters, including starting at 1 for numbered menus and converting a user-facing number to a zero-based list index.
Key takeaways
- Python list iteration processes each item in order, making a `for` loop suitable for repeated operations such as printing each fruit or measuring each string.
- A direct loop over a dictionary yields keys; retrieve each associated value with dictionary lookup, for example `diFruitPrices[strFruit]`.
- Multiple assignment unpacks nested records into variables in one loop, but each record must have the same number of fields as the loop expects.
- `enumerate()` supplies an index and item together, replacing a manually updated counter when displaying numbered choices.
- Because `enumerate()` starts at 0 by default while user-facing menus often start at 1, choose a start value of 1 for display or subtract 1 before using a menu selection as a list index.
Chapters
0:00
Iterating Through Python Lists and Dictionaries
- A `for` loop such as `for strFruit in liFruit` visits list items one at a time and in order, enabling actions such as printing each fruit or its string length.
- Iterating directly over a dictionary such as `diFruitPrices` yields its keys, for example `apple` and `pear`, rather than its values.
- Use the current key to retrieve a dictionary value, as in `diFruitPrices[strFruit]`; an f-string can combine the fruit name and its price without attempting to add incompatible types such as a string and a float.
3:35
Unpacking Nested Fruit Records with Multiple Assignment
- A list can hold nested records such as `[fruit, price]`, and a loop can unpack each record with `for strFruit, flPrice in liFruitPrices`.
- Multiple assignment can unpack records with additional fields, such as fruit, price, and quantity, into separate variables during each iteration.
- Every nested record must contain the expected number of values; an inconsistent record causes an unpacking error.
11:46
Numbering Collection Items with Python enumerate()
- `enumerate(liFruit)` pairs each fruit with a counter, avoiding a manually maintained counter and `+= 1` inside the loop.
- The counter starts at 0 by default; pass a starting value such as 1 to display menu choices as `1. apple`, `2. pear`, and so on.
- `enumerate()` produces an enumerate object that yields index-item pairs for multiple assignment, rather than printing as a list.
- When a user selects a one-based menu number for a list operation, subtract 1 to get the corresponding zero-based index.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.