Python (Fall 2026) Module 11-1: Iteration and Collections
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
- 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.
- 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.
- `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.
Transcript excerpt
0:00 Okay, so we've got one more single day module here, module 11, and this is another one of those things that is kind of small in scope, but just good to know as we move into the next part of the class. And then after this module, we're going to tackle some, a few more weeks, bigger subject matter. And then we'll wrap up the class with my favorite part, which is the four, the four projects at the end of the class that are more open-ended and fun and that sort of thing. But we do have some trickier ground to cover between now and then. This module, however, I would not put among the trickier things. I just think it's interesting and useful, and it'll help you sort of think through some of the things that we're doing and understand some of
0:54 the things that we're doing later in the class. So, before I jump into Python modules and what they are and how they work and all that stuff, I do want to very briefly review lists and iteration through lists, because this is, again, something that is sometimes confusing concept to people, something that's important to know. So, when we're talking about lists and iteration, what we're talking about is how we can go through the items of a list using a for loop and do something with them. So, a basic list iteration might look like this. For strFruit in liFruit. And what that does is it goes through this list one at a time in order.
1:51 And one at a time it will put the value of that item in the list inside the variable strFruit. And while we're looping through that, it's going to allow us to, to do something to each one of
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
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.