Working with Lists: Creating and Accessing Collections
A for loop is a definite loop that processes each item in a known collection exactly once.
A Collection in Motion
Suppose a collection contains the items red, green, and blue. A for loop can process those items in order, one item at a time. Because the collection is already known, the loop is a definite loop: it processes each item in the collection exactly once.
The For Statement
A for loop has two especially important parts: the iteration variable and the collection. The iteration variable holds the current item. As the loop moves through the collection, that variable changes automatically so that the loop body can work with the current item.
In this example, color is the iteration variable and colors is the known collection. The keywords for and in are required. The for statement ends with a colon, and the statement inside the loop is indented.
Tracing Each Iteration
Predicting the Printed Items
Trace the loop and determine what it prints.
animals = ["cat", "dog", "bird"]
for animal in animals:
print(animal)- 1Iteration 1
The iteration variable animal receives cat. The loop body prints cat.
- 2Iteration 2
The iteration variable changes to dog. The loop body prints dog.
- 3Iteration 3
The iteration variable changes to bird. The loop body prints bird.
The output appears in collection order: cat, dog, bird. Each item is processed once.
cat
dog
birdWhat do you think happens?
What will this loop print, and in what order?
steps = ["start", "middle", "finish"]
for step in steps:
print(step)Reveal answer
It prints start, then middle, then finish.
The iteration variable step receives the current item in collection order. The loop processes each known item exactly once.
Positions and Items
A collection can be considered as a group of items arranged in positions. A for loop is useful when the goal is to process every item without manually selecting each position. The loop receives the current item through its iteration variable and advances automatically to the next item.
Choosing Loop Types
| Question | for loop | while loop |
|---|---|---|
| When is it a good fit? | When the collection and its size are known | When the number of iterations is unknown |
| What controls progress? | The loop moves through the known collection | A condition determines whether iteration continues |
| How does the source describe it? | A definite loop | Better for unknown iteration counts |
The main distinction is whether the number of iterations is known in advance.
Use a for loop when you know the collection to process and therefore know the set of iterations in advance. A while loop is a better choice when the number of iterations is unknown. The choice is about how the loop's repetition is determined: a known collection for a for loop, or an unknown count for a while loop.
Mistakes Beginners Make
Common mistake Leaving out the required for or in keyword
What it looks likecolor colors: print(color)Why it failsThe loop syntax requires the keywords for and in.
Do this insteadfor color in colors: print(color)Common mistake Leaving out the colon after the for statement
What it looks likefor color in colors print(color)Why it failsThe for statement requires a colon before the indented loop body.
Do this insteadfor color in colors: print(color)Common mistake Failing to indent the loop body
What it looks likefor color in colors: print(color)Why it failsThe statements controlled by the loop must be indented.
Do this insteadfor color in colors: print(color)Common mistake Treating the iteration variable as a fixed value
What it looks likefor color in colors: print("red")Why it failsThe loop's iteration variable is intended to hold the current item and change automatically on each iteration.
Do this insteadfor color in colors: print(color)
Practice the Trace
Trace this loop before looking at the answer. Write down the iteration variable's value during each iteration and then write the output order.
plans = ["learn", "build", "review"]
for plan in plans:
print(plan)Hints
- List the collection items in their existing order.
- Assign the first item to plan, then move to the next item.
- The loop body prints the current value of plan.
Practice Solution
Trace the plans loop.
- 1First iteration
plan receives learn, so the loop prints learn.
- 2Second iteration
plan changes to build, so the loop prints build.
- 3Third iteration
plan changes to review, so the loop prints review.
The output order is learn, build, review.
Key Takeaways
- 1A for loop is a definite loop that processes each item in a known collection exactly once.
- 2The iteration variable holds the current item and changes automatically on each iteration.
- 3A for statement requires for, in, a colon, and a properly indented loop body.
- 4To trace a loop, follow the collection order and record the iteration variable's value at each step.
- 5Choose a for loop when the collection and iteration count are known; choose a while loop when the number of iterations is unknown.
At a glance
- A for loop processes each item in a known collection exactly once.
- The iteration variable receives the current item and changes automatically as the loop progresses.
- Tracing means following collection order, recording the iteration variable, and predicting the loop body's output.
- The keywords for and in, the colon, and indentation are all part of the required loop structure.
- Use for when the iterations are known through a collection; use while when the iteration count is unknown.