Concepts / Nested Data Structures and Multi-Level Iteration

Nested Data Structures and Multi-Level Iteration

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, a for loop provides a direct way to visit each item in turn. The loop variable receives the actual element value during each iteration. It does not automatically receive the element's index.

python
Output
red
green
blue
iteration 1iteration 2iteration 3[red, green, blue]outer listredcolorgreencolorbluecolor
What element does the loop variable contain as the loop moves from the first list element to the last?

Reading the Outer List

A list traversal visits the list's elements in order. On the first iteration, the loop variable contains the first element; on the next iteration, it contains the next element; and so on until every element has been visited. You usually do not need to access each element manually by index. If an index is specifically required, the source notes that you can use range(len(list)) instead.

Tracing Three Iterations

Determine the values assigned to item while traversing ["A", "B", "C"].

First iteration: item contains the first list element, "A".

Second iteration: item contains the second list element, "B".

Third iteration: item contains the third list element, "C".

The loop processes "A", then "B", then "C".

Nested Lists as Outer Elements

A nested list is still one element from the point of view of the outer list. If an outer list contains values and another list, the outer loop does not automatically visit the individual values inside that nested list. Instead, one iteration assigns the entire inner list to the loop variable.

second loopsecond loop[A, B]one outer elementAinner iterationBinner iteration
Does the outer loop process each value inside a nested list individually, or does it receive the entire inner list as one element?
outer iteration 1outer iteration 2inner iterationinner iterationinner iterationinner iterationouter list[[A, B], [C, D]][A, B]outer variableAinner variable[C, D]outer variableBinner variableCinner variableDinner variable
When an outer loop reaches an inner list, what does the second loop process?

Empty List Behavior

An empty list contains no elements to visit. Therefore, a for loop over an empty list performs zero iterations: the loop body never executes. This is expected behavior, not an error.

beginno elementempty list[]element availablenoloop endszero iterations
What happens to the loop body when the list contains no elements?
python
Output
finished

Predicting the Output

What do you think happens?

What values are printed, and in what order, when the loop visits ["first", "second", "third"]?

  • first, second, third
  • 0, 1, 2
  • third, second, first
Reveal answer

Answer: first, second, third

The loop variable receives each actual element value in list order. It does not automatically receive the indexes.

next iterationnext iterationfirstoutput 1secondoutput 2thirdoutput 3
Given a list and a for loop, what values are produced and in what order before the code is executed?
EASY

Predict the output before running this loop: values = ["north", "south"]. The loop prints each value as it visits the list.

Hints
  • The loop variable receives actual values rather than indexes.
  • Follow the list from its first element to its last element.

Mistakes with Loop Levels

  • Assuming the loop variable contains an index

    A standard for loop over a list assigns the actual element value to the loop variable.

    Fix: Reason about the values 10, 20, and 30. Use range(len(list)) when the index is needed.

  • Assuming an outer loop automatically reaches inner values

    Each inner list is one element of the outer list.

    Fix: Use a second loop when the individual elements inside each nested list must be visited.

  • Treating zero iterations as an error

    An empty list has no elements, so there is nothing for the loop to assign to its variable.

    Fix: Expect the loop body to execute zero times and continue with the code after the loop.

Before running a loop, write down the outer list's elements in order. For a nested list, keep each inner list grouped as one outer element. Only split that inner group into individual values when a second loop processes it.

Practice Traversal

MEDIUM

For each case, predict how many times the loop body executes and what the loop variable receives: a list containing four ordinary values; an empty list; and a list containing two nested lists. Then explain what additional loop is needed to reach values inside the nested lists.

Hints
  • Count the elements of the list being directly traversed.
  • An empty list has no element for the loop variable.
  • Treat each nested list as one element until a second loop traverses it.

Essential Takeaways

  1. A for loop visits list elements in order.
  2. The loop variable contains the actual element value, not the index.
  3. An empty list causes zero loop-body executions and is not an error.
  4. A nested list is one element of its outer list during outer-loop iteration.
  5. A second loop is needed to visit the individual elements inside each nested list.

Key Takeaways

  • A for loop traverses a list by assigning each actual element to its loop variable in order.
  • The loop variable is a value, not an index.
  • An empty list produces zero iterations.
  • A nested list remains one outer element until another loop traverses its contents.
  • Predict output by tracing the elements at the current loop level.