Concepts / Understanding List Basics

Understanding List Basics

List traversal comes in two main forms: simple for loops for reading elements, and index-based loops using range(len()) for updating elements.

  • Programming

Two Ways to Move Through a List

When a loop moves through a list, the right pattern depends on what you need to do. A simple for loop gives you each element's value directly, which is useful when you only need to inspect or display the elements. An index-based loop gives you each position, allowing you to read an element and update it in place.

items[10, 20, 30]items[10, 20, 30]value10, then 20, then 30index0, then 1, then 2for valuerange(len(items))
How does a simple for loop differ from an index-based loop when working with list elements?

Reading Values Directly

In a simple for loop, the loop variable receives each list element's value in turn. The loop moves through the list in element order, so the first iteration uses the first value, the next iteration uses the next value, and so on. This pattern is appropriate when the task is to inspect or display values rather than change the list.

python
Output
red
green
blue
redcolorgreencolorbluecolornext iterationnext iteration
In what order does a simple for loop visit the list's elements, and what value is available during each iteration?
Key point

The loop variable in a simple for loop represents the current value, not the position of that value. This makes the pattern convenient for reading, but it does not provide the position needed to replace an element in the list.

Generating Positions for Updates

When a list element must be changed, the loop needs access to that element's position. The pattern for i in range(len(list)) combines len() and range() to visit the positions of the list. The loop variable i represents the current index, and the list can be accessed through that index for reading or modification.

items[4, 8, 12]len(items)3range(3)positions0, 1, 2list indicesmeasureprovide endpointgenerate
How do len() and range() produce the sequence of positions used to access a list?
0418212items[0]items[1]items[2]
Which list element does each index from range(len(items)) refer to?
python

Here, i identifies a position rather than directly holding the element's value. The expression scores[i] reads the current element, and assigning a new value to scores[i] updates that position in the list.

Tracing an In-Place Update

What do you think happens?

What list results after this loop finishes?

  • [3, 6, 9]

  • [4, 7, 10]

  • [3, 7, 11]

Reveal answer
Answer:

[4, 7, 10]

The loop visits each position. At index 0, 3 becomes 4; at index 1, 6 becomes 7; at index 2, 9 becomes 10.

Worked example

Following Each Index

Start with values = [3, 6, 9]. For each index in range(len(values)), add 1 to the element at that index.

  1. 1
    Index 0

    The element at position 0 is 3. Replacing it with 3 + 1 changes the list to [4, 6, 9].

  2. 2
    Index 1

    The element at position 1 is 6. Replacing it with 6 + 1 changes the list to [4, 7, 9].

  3. 3
    Index 2

    The element at position 2 is 9. Replacing it with 9 + 1 changes the list to [4, 7, 10].

Result

The final list is [4, 7, 10].

Index 0[4, 6, 9]Index 1[4, 7, 9]Index 2[4, 7, 10]next indexnext index
What happens to each list element as the index-based loop moves through the positions?
Key point

During an updating traversal, the loop changes the list one position at a time. Earlier changes remain in the list while later indices are processed. To predict the final list, record the index visited, the value originally at that position, and the replacement value.

Mistakes in Traversal Patterns

  • Common mistake

    Using a simple value loop when the task requires changing list elements

    What it looks like
    for score in scores:
        score = score + 5
    Why it fails

    The loop variable receives a value directly, but this pattern does not use a list position to replace the corresponding element.

    Do this instead

    Use for i in range(len(scores)) and assign the replacement through scores[i].

  • Common mistake

    Confusing the index with the element value

    What it looks like
    for i in range(len(scores)):
        print(i)
    Why it fails

    This prints the generated positions rather than the values stored at those positions.

    Do this instead

    Use scores[i] when you need the element associated with the current index.

  • Common mistake

    Updating the wrong position while tracing the loop

    What it looks like

    Starting with [3, 6, 9], changing the element at index 1 during the index 0 iteration

    Why it fails

    Each iteration acts on the position held by the current index.

    Do this instead

    Track the index in order and update only the corresponding list position.

Practice the Trace

EASY

Trace the loop without running it. Write down the list after each index is processed, then give the final list.

values = [2, 5, 8]
for i in range(len(values)):
    values[i] = values[i] * 2
Hints
  • The index-based loop visits each position in order.
  • At each position, replace the current value with that value multiplied by 2.
  • Keep the changes from earlier iterations when processing the next index.
Worked example

Practice Check

Determine the final list for values = [2, 5, 8] when each element is replaced by twice its value.

  1. 1
    Index 0

    The value 2 becomes 4, giving [4, 5, 8].

  2. 2
    Index 1

    The value 5 becomes 10, giving [4, 10, 8].

  3. 3
    Index 2

    The value 8 becomes 16, giving [4, 10, 16].

Result

The final list is [4, 10, 16].

Traversal Checklist

  1. 1
    A simple for loop visits list values directly and is suited to inspecting or displaying elements.
  2. 2
    An index-based loop visits positions generated by range(len(list)).
  3. 3
    Use the index expression, such as list[i], when you need to replace an element in the list.
  4. 4
    To trace an update, follow each index in order and record the list after every replacement.
  5. 5
    The final list contains all changes made at the positions visited by the index-based loop.

Key Takeaways

  • Simple for loops provide list values directly for reading.
  • Index-based loops using range(len(list)) provide positions for reading and updating.
  • The expression list[i] connects the current index to the element that can be changed.
  • Tracing each index and intermediate list state makes final mutations predictable.

More concepts starting with U