Concepts / Understanding List Structure and Indexing

Understanding List Structure and Indexing

For loops are the most common and natural way to iterate through list elements, using identical syntax to string iteration.

  • Programming

One Element at a Time

When a list contains several items, you often need to process each item in turn. A for loop provides a natural way to visit every element without manually accessing each element by its index. The loop moves through the list in order, and its loop variable holds the actual element value for the current iteration.

What do you think happens?

Suppose a list contains the values red, green, and blue. What values will the loop variable receive as the loop visits the list?

  • The positions 0, 1, and 2
  • The values red, green, and blue
  • Only the first value
  • No values
Reveal answer

Answer: The values red, green, and blue

During direct list iteration, the loop variable holds the actual element value on each iteration, not the element's index.

Tracing List Traversal

A for loop traverses a list by repeatedly taking the next element and assigning that element to the loop variable. For a list with three elements, there are three iterations: the first iteration receives the first element, the second receives the second element, and the third receives the third element. The loop body runs once for each element.

next elementthenthenListred, green, bluerediteration 1greeniteration 2blueiteration 3
How does control move through each list element, and what value does the loop variable hold at each iteration?

Following Three Iterations

A list contains the elements apple, pear, and plum. Predict the value held by the loop variable during each iteration.

First iteration: The loop variable receives apple, the first element.

Second iteration: The loop variable receives pear, the next element.

Third iteration: The loop variable receives plum, the final element.

The loop variable receives apple, then pear, then plum. The loop body runs three times.

Values Versus Positions

In direct iteration over a list, the loop variable represents the actual element value, not the element's index. If a program needs the index, the source explains that it should use range(len(list)) instead. This creates an important distinction: direct iteration focuses on what each element is, while index-based iteration is used when the position is needed.

Iteration focusWhat the loop variable representsWhen it is useful
Direct list iterationThe actual element valueWhen you want to process each item
Index-based iterationAn index used to identify a positionWhen you need the list position; use range(len(list))
containscontainscontainsPosition 0first elementappleelement valuePosition 1second elementpearelement valuePosition 2third elementplumelement value
How do list positions map to their corresponding elements, and what does direct iteration receive?

Nested and Empty Lists

A nested list is still one element of its containing list. If an outer list contains ordinary values and another list, one iteration visits the nested list as a whole. The loop does not automatically visit the values inside that nested list individually during that single traversal.

containscontainsalphaouter elementbetainside nested listinner listbeta, gammagammainside nested listdeltaouter element
When a list contains another list, does one outer loop visit the inner values separately or treat the inner list as one element?

Counting Outer Iterations

An outer list contains alpha, a nested list containing beta and gamma, and delta. Predict the values received during one traversal of the outer list.

First iteration: The loop receives alpha.

Second iteration: The loop receives the entire nested list containing beta and gamma as one element.

Third iteration: The loop receives delta.

The outer traversal has three iterations, not four. Beta and gamma remain inside the nested-list element during this traversal.

An empty list contains no elements, so the loop body executes zero times. This is normal behavior, not an error. To predict the result, first determine how many elements the list contains: a nonempty list gives one iteration per element, while an empty list gives no iterations.

beginnonefinishEmpty listno elementsNext elementnone availableLoop bodyzero executionsLoop endsno iteration
What happens to the loop body when the list contains no elements?

Mistakes in Prediction

  • Assuming the loop variable stores an index

    The loop variable holds the actual element value, not the index.

    Fix: Predict the values in the list. If the index is required, use range(len(list)) instead.

  • Counting the values inside a nested list as separate outer iterations

    The nested list is counted as one element of the outer list.

    Fix: Count the nested list as one outer element during that traversal.

  • Treating an empty list as an error

    There is no element to assign to the loop variable.

    Fix: Expect zero loop-body executions for an empty list.

  • Predicting output without counting elements first

    The number of iterations depends on the elements available in the list.

    Fix: Count the outer list's elements, then write down the value received at each iteration in order.

Prediction Practice

MEDIUM

Consider an outer list with these three elements: north, a nested list containing east and west, and south. Before running a for loop over the outer list, write down the value received on each iteration and the total number of iterations. Then consider an empty list and predict how many times its loop body executes.

Hints
  • Count the nested list as one element of the outer list.
  • The loop variable receives element values rather than indexes.
  • An empty list has no element available for an iteration.

What do you think happens?

For the outer list containing north, a nested list containing east and west, and south, what are the three values received during the outer traversal?

Reveal answer

Answer: The values are north, the entire nested list containing east and west, and south.

The nested list is one element of the outer list, so the outer loop has three iterations. An additional traversal would be needed to visit the nested values individually.

Key Takeaways

  1. A for loop visits list elements in order, once per element.
  2. The loop variable holds the actual element value, not its index.
  3. Use range(len(list)) when the index is needed.
  4. A nested list is one element during traversal of its containing list.
  5. An empty list causes the loop body to execute zero times.

Key Takeaways

  • Direct list iteration gives the loop variable each actual element value in order.
  • The number of loop iterations matches the number of elements in the list being traversed.
  • A nested list counts as one element of its outer list.
  • An empty list makes the loop body run zero times.
  • Predict the element sequence before running the loop, and distinguish values from indexes.