Concepts / Iterating Through Lists with Loops

Iterating Through Lists with Loops

The central idea is simple: a for loop takes the items of a list in sequence and executes its body once for each item.

  • Programming
Interactive lab

Try it: Loop Tracer

How a Python for loop visits each item in turn, and how an accumulator variable (a total, a count, a best-so-far or a result list) changes on every iteration.

How it works

  1. Initialise the accumulator before the loop.
  2. Each iteration, the loop variable takes the next value from the list.
  3. An if inside the loop decides whether to update the accumulator.
  4. After the last item the loop ends and the accumulator holds the answer.

Default run (13 steps): values = [4, 9, 2, 7, 5]. Run the loop one statement at a time. … The loop has used every value. print(total) shows 27.

Simplified: A model of five fixed loop programs (sum, count, maximum, minimum, filter) run on your list — it steps the program exactly as Python would, but it does not run Python.

Educational simulation

Loading the simulation…

One List, Many Actions

Suppose you have a list of words, numbers, or other items and want to perform the same action for every item. A for loop provides a direct way to move through that known collection. Instead of describing a separate action for each item, you describe one loop body, and that body runs once for each item in the list.

Key point

The central idea is simple: a for loop takes the items of a list in sequence and executes its body once for each item.

The List as an Ordered Sequence

A list is also a sequence. Its items are arranged as an ordered collection, so iteration visits one item after another rather than treating the list as an unstructured group. The loop continues through the known items until it has processed the list.

first itemposition 1second itemposition 2third itemposition 3nextnext
How are the items arranged in the list, and how does the loop visit them in sequence?
In the real world

For example, a list of words can be processed word by word, and a list of numbers can be processed number by number. The same loop structure applies because both are lists of items to visit.

The Loop's Progression

At the beginning of a for loop, the list supplies the items that will be visited. The loop selects the first item and runs its body. It then moves to the next item and runs the same body again. This continues until every item in the list has been processed.

listknown set of itemsfirst itemrun bodynext itemrun bodylast itemrun bodyloop endsall items processedselectmove to nextcontinuefinish
What happens next as the loop moves from one list item to the next, and when does the loop stop?
Worked example

Tracing Three Items

Imagine a list containing three words. Trace how a for loop processes the list.

  1. 1
    First iteration

    The loop takes the first word and executes the loop body once for that word.

  2. 2
    Second iteration

    The loop moves to the second word and executes the same body once more.

  3. 3
    Third iteration

    The loop takes the third word and executes the body for the final item.

  4. 4
    Completion

    There are no more items in the known list, so the loop stops.

Result

The body runs three times, once for each word in the list.

What do you think happens?

If a list contains three items, how many times does the body of a for loop run when it processes that list?

  • Once

  • Twice

  • Three times

  • Until a condition becomes False

Reveal answer
Answer:

Three times

A for loop processes the known set of items, so its body runs once for each of the three items.

Definite and Indefinite Repetition

Loop typeWhat controls repetitionRelationship to a list
for loopA known set of itemsRuns once for each item in the set
while loopA conditionContinues until the condition becomes False

The source distinguishes the for loop as definite repetition and the while statement as indefinite repetition.

A for loop is called definite because the loop is working through a known set of items. If the list has three items, the loop has three item visits to carry out. A while loop is different in this respect: it keeps looping until its condition becomes False. The important distinction is what determines the repetition: the items in the known set for a for loop, and a condition for a while loop.

Scanning for a Useful Value

Iteration is useful when the goal is not merely to visit items but to inspect them for a result. A loop can go through a list of values while looking for something such as the largest or smallest value. The repeated scan gives the program an opportunity to consider each item in the collection.

Worked example

Finding an Extreme Value

A list contains several numbers, and the task is to look through the list for the largest value.

  1. 1
    Visit the first number

    The loop begins scanning the list by processing its first item.

  2. 2
    Visit each following number

    The loop continues through the remaining items, considering each one in sequence.

  3. 3
    Compare the scanned values

    The process is looking for the largest value among the data that has been scanned.

  4. 4
    Finish the scan

    After the final item has been processed, the loop has examined the known list.

Result

A for loop can scan every item in a list when searching for the largest or smallest value.

Key point

The loop does not need a different body for each number. The same body is applied as the loop moves from one list item to the next.

Mistakes in Loop Tracing

  • Common mistake

    Treating the list as an unordered group

    What it looks like

    Reasoning that the loop can visit the items in any order.

    Why it fails

    A list is a sequence, and iteration moves through its items in sequence.

    Do this instead

    Trace the first item, then the next item, continuing until the final item.

  • Common mistake

    Assuming the loop body runs only once

    What it looks like

    Seeing one loop body and expecting one total execution.

    Why it fails

    The body executes once for each item in the list.

    Do this instead

    Count the items in the known set and use that count to trace the iterations.

  • Common mistake

    Describing a for loop as condition-controlled repetition

    What it looks like

    Saying that a for loop continues until a condition becomes False.

    Why it fails

    That description belongs to the source's explanation of a while statement. A for loop goes through a known set of items.

    Do this instead

    Describe a for loop as definite repetition over the items in a known set.

  • Common mistake

    Stopping before the final item

    What it looks like

    Processing the first two items of a three-item list and declaring the loop finished.

    Why it fails

    The loop runs through as many iterations as there are items in the set.

    Do this instead

    Continue tracing until every item has been visited.

Edge case

The reliable stopping rule for this topic is collection-based: once the for loop has processed every item in the known list, its list iteration is complete. Do not replace that rule with the while-loop rule about waiting for a condition to become False.

Practice the Trace

EASY

A list contains five items. Without writing code, describe the order in which a for loop visits them and state how many times the loop body runs.

Hints
  • A list is a sequence, so begin with the first item.
  • The loop moves to the next item after each body execution.
  • The number of item-based iterations matches the number of items in the known set.
MEDIUM

A list of numbers is being scanned to find the smallest value. Explain why the loop must process every item before the scan is complete.

Hints
  • The loop is looking for a value among the data being scanned.
  • The list supplies a known set of items to visit.
  • The final item marks completion of the list scan.

The Iteration Pattern

  1. 1
    A list is a sequence of ordered items.
  2. 2
    A for loop visits the list items in sequence.
  3. 3
    The loop body runs once for each item in the known set.
  4. 4
    A for loop is definite because its repetition is based on a known set of items.
  5. 5
    A loop can scan list data when searching for values such as the largest or smallest item.

Key Takeaways

  • A for loop iterates through the items of a list in sequence.
  • Its body runs once for every item in the known list.
  • The loop stops after the final item has been processed.
  • This definite behavior differs from a while loop, which continues until its condition becomes False.
  • Iteration can be used to scan list data for values such as the largest or smallest item.

Related concepts

More concepts starting with I