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.

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

Following Three Iterations

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

  1. 1
    First iteration

    The loop variable receives apple, the first element.

  2. 2
    Second iteration

    The loop variable receives pear, the next element.

  3. 3
    Third iteration

    The loop variable receives plum, the final element.

Result

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

Values Versus Positions

Definition

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))

The central difference between visiting values directly and working through positions.

Position 0first elementappleelement valuePosition 1second elementpearelement valuePosition 2third elementplumelement valuecontainscontainscontains
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.

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

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.

  1. 1
    First iteration

    The loop receives alpha.

  2. 2
    Second iteration

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

  3. 3
    Third iteration

    The loop receives delta.

Result

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.

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

Mistakes in Prediction

  • Common mistake

    Assuming the loop variable stores an index

    What it looks like

    Treating the loop variable as position 0, position 1, and position 2 during direct list iteration.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Counting the values inside a nested list as separate outer iterations

    What it looks like

    Expecting an outer list containing alpha, a nested list of beta and gamma, and delta to produce four outer iterations.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Treating an empty list as an error

    What it looks like

    Expecting the loop body to run once even though the list has no elements.

    Why it fails

    There is no element to assign to the loop variable.

    Do this instead

    Expect zero loop-body executions for an empty list.

  • Common mistake

    Predicting output without counting elements first

    What it looks like

    Reading the loop body before identifying how many elements the list contains.

    Why it fails

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

    Do this instead

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

At a glance

  • 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.

Related concepts

More concepts starting with U