Concepts / Mutating Lists: pop(), append(), and extend()

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.

  • Programming
Interactive lab

Try it: List Methods, Step by Step

What append, extend, insert, remove, pop, del, index, count, sort and reverse really do to a Python list — which items shift, what each returns, CPython's exact error messages — and why lst = lst + other makes a new list while += changes it in place.

How it works

  1. append/extend add at the end; nothing else moves.
  2. insert(i, x) resolves i (negative counts from the end; out of range is clamped, never an error), shifts the tail right, then places x.
  3. remove(x) scans for the FIRST equal item (ValueError if none); pop(i) and del lst[i] take one position (IndexError if out of range); the tail shifts left.
  4. index/count scan and return a number; sort/reverse rearrange in place and return None.
  5. lst + other builds a new list object and rebinds the name; lst += other extends the same object.

Default run (5 steps): lst = [30, 10, 20, 10] — list object #1, 4 items. Choose a method. Example: lst.insert(1, 99). … lst.insert(1, 99) returned None. lst is now [30, 99, 10, 20, 10].

Simplified: A list of up to 12 ints; sort() is shown as its result, not step by step.

Educational simulation

Loading the simulation…

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.

Key point

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.

aindex 0aindex 0bindex 1cindex 1cindex 2remainsshifts left
What changes when remove() searches for 'b'?
Definition

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.

Worked example

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?

  1. 1
    Search

    remove() searches for the value 'b' in the list.

  2. 2
    Mutate

    The first matching value is deleted, leaving 'a' and 'c' in the list.

  3. 3
    Check the result

    The operation returns None rather than the removed value.

Result

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

index 0aindex 1bindex 2cindex 3dindex 4eindex 5fselected by 1:5
Which list positions are selected by the slice from index 1 up to, but not including, index 5?
Key point

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.

index 1bindex 5findex 2cindex 3dindex 4eindex 5fend boundary excluded
Why does del t[1:5] remove index 1 but preserve index 5?

Value or Position

OperationWhat you provideWhat it removes
remove(value)A valueThe first occurrence of that value
del t[index]One indexThe element at that position
del t[start:end]A start and end boundaryAll 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.

  • Common mistake

    Using remove() when you need to delete a specific position.

    What it looks like

    Choosing remove() when the intended target is the element at a known index.

    Why it fails

    remove() searches by value, not by position.

    Do this instead

    Use del with the known index.

  • Common mistake

    Expecting remove() to return the deleted value.

    What it looks like

    Saving the result of remove() and expecting it to contain the removed element.

    Why it fails

    remove() returns None even though it changes the list.

    Do this instead

    Inspect the changed list separately; do not treat the return value as the removed element.

  • Common mistake

    Including the end boundary of a deletion slice.

    What it looks like

    Interpreting del t[1:5] as removing index 5 too.

    Why it fails

    The right boundary is exclusive.

    Do this instead

    Count indices 1, 2, 3, and 4; index 5 is not selected.

  • Common mistake

    Assuming a range deletion leaves later indices unchanged.

    What it looks like

    Expecting the element originally at index 5 to remain at index 5 after del t[1:5].

    Why it fails

    The deleted elements leave a gap, and the remaining elements shift to fill it.

    Do this instead

    Re-evaluate the positions of surviving elements after the deletion.

Check Your Reasoning

EASY

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.
Worked example

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

  1. 1
    Value-based choice

    If the only information available is the value 'd', remove() is the matching operation because it searches for a value.

  2. 2
    Range-based choice

    If the intended positions are 1 through 4, del t[1:5] selects those four consecutive indices.

  3. 3
    Compare the scope

    The first operation targets the first matching value, while the second targets every element in the specified consecutive range.

Result

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

  1. 1
    remove() deletes the first occurrence of a known value.
  2. 2
    remove() modifies the list in place and returns None.
  3. 3
    del with slice notation removes multiple consecutive elements.
  4. 4
    For del t[start:end], start is included and end is excluded.
  5. 5
    Use remove() for value-based selection and del with an index or slice for position-based selection.

At a glance

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

Related concepts

More concepts starting with M