Concepts / Iterating Through Lists

Iterating Through Lists

The assignment changes an element in the existing list. It does not create a new list.

  • Programming

A List Can Change

A list is not limited to the contents it had when it was created. Lists are mutable, which means their contents can be changed after creation. One way to change a list is to reassign the element at a particular index using bracket notation on the left side of an assignment.

Key point

The assignment changes an element in the existing list. It does not create a new list.

scores[8, 6, 9]scores[8, 10, 9]reassign one element
How can the same list object contain different elements before and after modification?

Index Positions and Elements

A list represents a mapping between indices and elements. Each index position corresponds to exactly one element. When you work with a list, the index identifies the position whose element you want to inspect or reassign.

Worked example

Reading an Index-to-Element Mapping

Consider a list named colors with the elements red, blue, and green. Identify the element associated with each position.

  1. 1
    Identify the positions

    The list has one index position for each element.

  2. 2
    Match each position

    The first position maps to red, the second position maps to blue, and the third position maps to green.

  3. 3
    Use the mapping

    An index selects its corresponding element. Reassigning through that index targets that element's position in the list.

Result

Each index position identifies exactly one element in the list.

Index 0redIndex 1blueIndex 2greenmaps to redmaps to bluemaps to green
Which element does each index refer to, and how does the index position map to the list's contents?

Reassigning by Position

Definition

To modify a particular list element, place the list name and index on the left side of an assignment. The bracket notation identifies which element is being reassigned.

Worked example

Changing One Element

Suppose a list named tasks contains the elements plan, test, and review. Change the element at the position associated with test so that it becomes revise.

  1. 1
    Locate the target

    Use the index-to-element mapping to identify the position associated with test.

  2. 2
    Place the target on the left

    Write the list name with that index in bracket notation on the left side of an assignment.

  3. 3
    Assign the replacement

    Assign revise to that indexed position.

  4. 4
    Check the result

    The list now contains plan, revise, and review. The other elements remain part of the list.

Result

The selected element is replaced in the existing list.

tasks[plan, test, review]tasks[plan, revise, review]reassign selected position
What changes in the list when an assignment uses list[index] on the left side?

Tracing List Iteration

To trace an iteration through a list, follow the list's index-to-element mapping one position at a time. At each position, identify the element associated with that index. This gives you a concrete way to track which list element is being considered next.

redfirst elementbluenext elementgreennext elementadvanceadvance
What element does the loop visit next as it progresses through the list?
Key point

Iteration becomes easier to reason about when you separate two questions: which index position is being considered, and which element does that position map to?

Membership with in

The in operator checks whether a particular value exists in a list. It returns True when the value is present and False when the value is not present.

Worked example

Checking for an Element

A list named colors contains red, blue, and green. Determine the result of checking for blue and checking for yellow.

  1. 1
    Check blue

    Blue exists as an element in the list, so the membership check returns True.

  2. 2
    Check yellow

    Yellow does not exist as an element in the list, so the membership check returns False.

Result

The in operator distinguishes present values from absent values by returning True or False.

bluevalue being checkedIn listmembership testTruevalue existsFalsevalue absentuse inpresentnot present
How does the in operator determine whether a particular element exists in the list?

Mistakes to Avoid

  • Common mistake

    Treating a list as fixed after creation.

    What it looks like

    Assuming that the original elements cannot be changed.

    Why it fails

    Lists are mutable, so their contents can be modified after creation.

    Do this instead

    Remember that an indexed element can be reassigned.

  • Common mistake

    Forgetting to put the indexed list expression on the left side of the assignment.

    What it looks like

    Describing a target position without using the list and its index as the assignment target.

    Why it fails

    Bracket notation on the left side identifies the list element being reassigned.

    Do this instead

    Use the list name with the target index on the left side, then place the replacement value on the right side.

  • Common mistake

    Confusing an index with the element stored there.

    What it looks like

    Treating the position identifier and the value at that position as interchangeable.

    Why it fails

    A list represents a mapping between index positions and elements; each index corresponds to an element.

    Do this instead

    First identify the index position, then identify the element mapped to it.

  • Common mistake

    Using an index-based idea when the question is whether a value exists.

    What it looks like

    Trying to answer whether a value is present by naming a position.

    Why it fails

    The in operator is the operation that checks membership and returns True or False.

    Do this instead

    Use in when the goal is to test whether a value exists in the list.

Apply the Mechanism

MEDIUM

Imagine a list named animals containing cat, dog, and bird. Explain how you would identify the position associated with dog, replace that element with rabbit, and check whether rabbit exists in the list after the change.

Hints
  • Start with the index-to-element mapping.
  • Use bracket notation on the left side of an assignment to target the selected position.
  • Use the in operator to check membership after the reassignment.

What do you think happens?

A list contains cat, dog, and bird. After the element dog is reassigned to rabbit, does the membership check for rabbit return True or False?

  • True

  • False

Reveal answer
Answer:

True

Reassignment changes the selected element in the existing list, so rabbit exists in the list after the change.

Key Takeaways

  1. 1
    Lists are mutable, so their contents can change after creation.
  2. 2
    Bracket notation on the left side of an assignment selects the list element to reassign.
  3. 3
    A list maps index positions to elements, with each index corresponding to one element.
  4. 4
    Reassignment modifies the existing list in place rather than creating a new list.
  5. 5
    The in operator checks membership and returns True when a value exists and False when it does not.

At a glance

  • Lists are mutable and can be modified after they are created.
  • Use an indexed list expression on the left side of an assignment to replace one element in place.
  • Use the index-to-element mapping to identify which value belongs to each position.
  • Use in to test whether a value exists in a list.
  • Distinguish selecting a position by index from checking for a value by membership.

Related concepts

More concepts starting with I