BackSection Summary: Lists

Activities 1.18 – 1.23. Use this to review, or to fill in a definition you missed.
1. Lists and Indexing
A list holds many values in one variable, in order. You reach any one of them by its position, counting from zero.
- List
- A single variable that stores multiple values in order.
- Element
- One value stored inside a list.
- Index
- The position of an element in a list.
- Zero-Based Indexing
- The first element of a list is at index
0, not 1.
2. Length and Safe Access
The last valid index is always one less than the length. Computing it instead of hardcoding it is what keeps your code working when the list changes size.
- len()
- Built-in function that returns how many elements a list has.
- Off-by-One Error
- A mistake where an index is one too high or one too low — often from forgetting
len(list) - 1.
3. Traversal and Growth
A loop can visit every element in turn, either by value or by index. A list can also grow while the program is running.
- Traversal
- Visiting every element of a list, one at a time.
- append()
- Adds a new element to the end of a list.
4. The Accumulator Pattern
Totals, counts, and averages all come from the same shape: start a variable at a base value, then update it on every pass of the loop.
- Accumulator
- A variable that starts at a base value and builds up a result across a loop, like a running total or a count.
5. Membership and Position
Ask whether a value is in a list before you ask where it is — .index() crashes on a value that isn't there.
- in Operator
- Checks whether a value exists in a list, returning
True or False. - .index()
- Returns the position of a value in a list — crashes if the value isn't found.
6. Parallel Lists
Two lists kept in the same order let one index pull related values out of both — a question from one list, its answer from the other.
- Parallel Lists
- Two or more lists where the same index refers to related values across all of them.