Concepts / Iterating Through Lists

Iterating Through Lists

Lists are mutable: you can change their contents after creation by reassigning elements using bracket notation on the left side of an assignment.

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

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

reassign one elementscores[8, 6, 9]scores[8, 10, 9]
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.

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.

Identify the positions: The list has one index position for each element.

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

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

Each index position identifies exactly one element in the list.

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

Reassigning by Position

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.

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.

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

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

Assign the replacement: Assign revise to that indexed position.

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

The selected element is replaced in the existing list.

reassign selected positiontasks[plan, test, review]tasks[plan, revise, review]
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.

advanceadvanceredfirst elementbluenext elementgreennext element
What element does the loop visit next as it progresses through the list?

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.

Checking for an Element

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

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

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

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

use inpresentnot presentbluevalue being checkedIn listmembership testTruevalue existsFalsevalue absent
How does the in operator determine whether a particular element exists in the list?

Mistakes to Avoid

  • Treating a list as fixed after creation.

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

    Fix: Remember that an indexed element can be reassigned.

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

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

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

  • Confusing an index with the element stored there.

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

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

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

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

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

Key Takeaways

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