Mutating Lists: pop(), append(), and extend()
The remove() method deletes the first occurrence of a value from a list when you know the value but not its index.
Choosing What to Remove
List removal depends on the information you have. If you know the value you want to remove but not its position, use remove(). If you know the position, del can remove one element by index. If you need to remove several consecutive positions, del can use slice notation.
The central distinction is value versus position: remove() searches for a value, while del uses an index or a range of indices.
Tracing remove()
Suppose a list contains the values 'a', 'b', and 'c'. Calling remove('b') searches the list for the value 'b'. Once that value is found, it is deleted. The remaining values are 'a' and 'c', so the list becomes shorter by one element.
The remove() method deletes the first occurrence of a value from a list when you know the value but not its index.
The Return Value
remove() changes the existing list in place. It does not return the element that was deleted; its return value is None. Therefore, assigning the result of remove() to a variable gives that variable None, while the original list has still been changed.
Separating the Changed List from the Return Value
A list contains 'a', 'b', and 'c'. You remove 'b' and capture the result of the operation. What does the list contain afterward, and what value does the captured result contain?
Search: remove() searches for the value 'b' in the list.
Mutate: The first matching value is deleted, leaving 'a' and 'c' in the list.
Check the result: The operation returns None rather than the removed value.
The list contains 'a' and 'c', and the captured return value is None.
Deleting a Consecutive Range
Use del with slice notation when several consecutive elements must be removed in one operation. For a list containing 'a', 'b', 'c', 'd', 'e', and 'f', the expression del t[1:5] removes the elements at indices 1, 2, 3, and 4. Those values are 'b', 'c', 'd', and 'e'.
After del t[1:5], 'a' remains at the beginning and 'f' remains at the end. The surviving 'f' shifts from its original index 5 to index 1 because the earlier elements were deleted.
Reading Slice Boundaries
A deletion slice follows the same boundary rule as the slice notation shown in the source: the start boundary is included and the end boundary is excluded. In del t[1:5], index 1 is included, while index 5 is not. The selected indices are therefore 1, 2, 3, and 4.
Value or Position
| Operation | What you provide | What it removes |
|---|---|---|
| remove(value) | A value | The first occurrence of that value |
| del t[index] | One index | The element at that position |
| del t[start:end] | A start and end boundary | All elements from start through the position before end |
Choose the operation according to whether your information is a value, one position, or a consecutive range of positions.
Using remove() when you need to delete a specific position.
remove() searches by value, not by position.
Fix:
Use del with the known index.Expecting remove() to return the deleted value.
remove() returns None even though it changes the list.
Fix:
Inspect the changed list separately; do not treat the return value as the removed element.Including the end boundary of a deletion slice.
The right boundary is exclusive.
Fix:
Count indices 1, 2, 3, and 4; index 5 is not selected.Assuming a range deletion leaves later indices unchanged.
The deleted elements leave a gap, and the remaining elements shift to fill it.
Fix:
Re-evaluate the positions of surviving elements after the deletion.
Check Your Reasoning
A list contains six values in order: 'a', 'b', 'c', 'd', 'e', 'f'. Determine which values are removed by del t[1:5], which value remains at the final position after the deletion, and whether remove() would be the right tool if you only knew that the value to delete was 'd'.
Hints
- Write out the indices from 0 through 5.
- For the slice 1:5, include 1 and stop before 5.
- remove() is selected by value, while del with a slice is selected by position.
Applying Both Selection Rules
Use the source list 'a', 'b', 'c', 'd', 'e', 'f' to compare a value-based deletion of 'd' with the range deletion del t[1:5].
Value-based choice: If the only information available is the value 'd', remove() is the matching operation because it searches for a value.
Range-based choice: If the intended positions are 1 through 4, del t[1:5] selects those four consecutive indices.
Compare the scope: The first operation targets the first matching value, while the second targets every element in the specified consecutive range.
remove() is appropriate when the target is identified by value. del with a slice is appropriate when the target is identified as a consecutive range of positions.
Key Takeaways
- remove() deletes the first occurrence of a known value.
- remove() modifies the list in place and returns None.
- del with slice notation removes multiple consecutive elements.
- For del t[start:end], start is included and end is excluded.
- Use remove() for value-based selection and del with an index or slice for position-based selection.
Key Takeaways
- Use remove(value) when you know what value to delete but not where it appears.
- Only the first occurrence is removed, and the list is changed in place.
- Use del with a slice to delete a consecutive range of positions.
- Slice deletion includes the start boundary and excludes the end boundary.
- Choose between remove() and del by asking whether your target is identified by value or by position.