Skip to content

Traversing lists

Walk through a list by value and by index to sum, find the largest and its position, change items in place and build a filtered list, and say which walk each job needs.

It opens on

Here the second loop doubles each t in temps. When it finishes, will print(temps) show the doubled list or the same list as before?

Why it matters

Almost every program that holds a list then loops over it, to total it, search it, clean it or change it; choosing between walking by value and by index, and not changing a list while walking it, is the difference between a few clean lines and a quiet wrong answer.

Good to know first

  • Lists: indexing and mutation: temps[i] reads a cell and temps[i] = v changes it
  • For loops and range: range(len(temps)) gives 0 to len(temps) - 1
  • Loop patterns: an accumulator starts at 0 and a running maximum compares each item

Step by step

  1. Walk by value

    Trace a for loop over a list that sums its items, and say why assigning to the loop variable does not change the list.

    On the first run t names 14, so total becomes 0 + 14, which is 14. On the second t names 22 and total becomes 36.

    the first rows of the trace

    A common mix-up

    t = t * 2 inside the loop doubles the items of the list.

    In fact: t is only a name for the current item, so assigning to t changes nothing in the list.

  2. Walk by index

    Loop over range(len(temps)) to find the position of the largest item and to change items in place, and say when the index is needed.

    best is the index of the largest item seen so far and starts at 0. At i = 0 the test compares temps[0] with itself, so best stays 0. At i = 1, temps[1] is 22, which is more than temps[0] = 14, so best becomes 1.

    the first row of the trace

    A common mix-up

    For t in temps and for i in range(len(temps)) differ only in style.

    In fact: By value the loop cannot change cells or say where an item was, while by index it can.

  3. Build a new list, and the traps

    Filter a list into a new one with append, predict what a loop that removes from its own list does, and tell sorted from sort.

    The run printed [22, 31, 27] 3. temps itself was not changed, so we have both lists, and a pass over n items costs O(n).

    A common mix-up

    Removing items inside for r in readings is a safe way to clean a list.

    In fact: Items after a removal slide left and are skipped, so some unwanted items survive.

Try it

What does this print: total = 0, for n in [2, 4, 6], total = total + n * n, print(total)?Answer

56

Hint: 4 + 16 + 36

For xs = [5, 9, 9, 2], the loop above gives best = 1. What does it give if the test is temps[i] >= temps[best]?Answer

2

Hint: The second 9 now passes the test

Write a loop that builds a new list of the items over 10 from [3, 18, 25, 9, 31].Answer

Big = [], for x in [3, 18, 25, 9, 31]: if x > 10: big.append(x), giving [18, 25, 31]

Hint: Start with an empty list and append

The third item is 9. What are t and total on the third run?Answer

t is 9 and total is 45.

At i = 2, temps[2] is 9. What is best afterwards?Answer

1, since 9 is not more than temps[best], which is 22.

Traversing lists

30 minutes every month. No card.