Iterating Through Lists
The assignment changes an element in the existing list. It does not create a new list.
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.
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.
- 1Identify the positions
The list has one index position for each element.
- 2Match each position
The first position maps to red, the second position maps to blue, and the third position maps to green.
- 3Use 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.
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.
- 1Locate the target
Use the index-to-element mapping to identify the position associated with test.
- 2Place the target on the left
Write the list name with that index in bracket notation on the left side of an assignment.
- 3Assign the replacement
Assign revise to that indexed position.
- 4Check 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.
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.
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.
- 1Check blue
Blue exists as an element in the list, so the membership check returns True.
- 2Check 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.
Mistakes to Avoid
Common mistake Treating a list as fixed after creation.
What it looks likeAssuming that the original elements cannot be changed.
Why it failsLists are mutable, so their contents can be modified after creation.
Do this insteadRemember 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 likeDescribing a target position without using the list and its index as the assignment target.
Why it failsBracket notation on the left side identifies the list element being reassigned.
Do this insteadUse 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 likeTreating the position identifier and the value at that position as interchangeable.
Why it failsA list represents a mapping between index positions and elements; each index corresponds to an element.
Do this insteadFirst 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 likeTrying to answer whether a value is present by naming a position.
Why it failsThe in operator is the operation that checks membership and returns True or False.
Do this insteadUse in when the goal is to test whether a value exists in the list.
Apply the Mechanism
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?
Reveal answer
True
Reassignment changes the selected element in the existing list, so rabbit exists in the list after the change.
Key Takeaways
- 1Lists are mutable, so their contents can change after creation.
- 2Bracket notation on the left side of an assignment selects the list element to reassign.
- 3A list maps index positions to elements, with each index corresponding to one element.
- 4Reassignment modifies the existing list in place rather than creating a new list.
- 5The 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.