List Methods: append, remove, and extend
Lists are mutable: you can change their contents after creation by reassigning elements using bracket notation on the left side of an assignment.
Changing Existing Contents
A list is mutable, which means its contents can be changed after the list has been created. One way to change an existing element is to place bracket notation on the left side of an assignment. This reassigns the element at the selected index within the existing list rather than creating a new list.
The important distinction is between selecting an element and changing the selected element. The index selects a position; the assignment gives that position a new value.
Checking Membership
The in operator checks whether a value exists in a list. Its result is True when the value exists and False when it does not. This is a membership check: it asks whether the value is among the list's elements, rather than selecting one element by index.
| Operation | Question it answers | Result described by the source |
|---|---|---|
| Index selection | Which element is associated with this position? | One index corresponds to one element |
| Membership check with in | Does this value exist in the list? | True or False |
| Reassignment | What happens when a selected position is placed on the left of an assignment? | The existing list is modified in place |
Named List Methods
This article is titled around append, remove, and extend, but the supplied source material does not define the behavior, arguments, return values, or index effects of those three methods. The supplied material does establish the broader idea that lists are mutable and that their contents can change after creation. Do not infer method-specific behavior from mutability alone: a complete explanation of each named method requires method-specific source information.
Mistakes to Avoid
Treating a list as unchangeable after creation
The source defines lists as mutable.
Fix:
Remember that bracket notation on the left side of an assignment can reassign an element.Confusing an index with the element at that index
A list represents a mapping in which each index position corresponds to one element.
Fix:
Use the index to identify the position, then reason about the element mapped to that position.Using in as though it selected an index
The in operator checks whether a value exists and returns True or False.
Fix:
Use in for membership questions and index notation for position-based selection.Assuming the supplied source specifies what append, remove, and extend do
The source names those methods in the article topic but does not provide their method-specific behavior.
Fix:
Consult method-specific documentation before making claims about their effects, arguments, or index changes.
Check Your Understanding
Explain the difference between these three questions: Which element is associated with a particular index? Does a particular value exist in the list? What happens when the selected list position is placed on the left side of an assignment?
Hints
- Relate the first question to the index-to-element mapping.
- Relate the second question to the in operator.
- Relate the third question to mutability and in-place modification.
Key Takeaways
- Lists are mutable, so their contents can change after creation.
- An index identifies a position, and each index corresponds to one list element.
- Bracket notation on the left side of an assignment reassigns the selected element in the existing list.
- The in operator checks membership and returns True when a value exists or False when it does not.
- The supplied source does not provide enough method-specific information to explain the exact behavior of append, remove, and extend.
Key Takeaways
- Lists can be modified after they are created because they are mutable.
- Indexes map positions to individual list elements.
- Reassignment through bracket notation changes the existing list in place.
- The in operator answers whether a value is present in the list.
- Append, remove, and extend need additional method-specific definitions before their exact behaviors can be taught accurately.