Concepts / Removing Elements by Value with remove

Removing Elements by Value with remove

The central difference is simple: pop removes by index and returns the removed value; del removes by index without returning it.

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

The Choice Behind Removal

When you need to remove an element from a particular position in a Python list, two tools are especially important: pop and del. Both change the original list in place, but they communicate different intentions. Use pop when the removed value still matters to your program. Use del when you simply want to discard the element.

Key point

The central difference is simple: pop removes by index and returns the removed value; del removes by index without returning it.

Following an Element Through pop

What do you think happens?

A list contains ['a', 'b', 'c']. If pop is used at index 1, what happens to the list and what value can the program keep?

  • The list stays the same and no value is available

  • The list becomes ['a', 'c'] and the removed value is 'b'

  • The list becomes ['b', 'c'] and the removed value is 'a'

Reveal answer
Answer:

The list becomes ['a', 'c'] and the removed value is 'b'.

Index 1 identifies 'b'. pop removes that element, shortens the list, and returns 'b', so the program can assign the returned value to a variable.

t['a', 'b', 'c']t['a', 'c']bindex 1x'b'pop(1)returned value
What changes in the list, and where does the removed value go, when index 1 is removed with pop?

The index supplied to pop identifies the position to remove. In the source example, pop(1) removes 'b' from the list ['a', 'b', 'c']. The shortened list is ['a', 'c'], and the returned value is 'b'. Assigning that return value to a variable allows the program to use the removed element later.

Tracking the Index Shift

Removing an element changes the index positions of elements that were to its right. Every later element shifts one position to the left, so its index decreases by one. In ['a', 'b', 'c'], 'a' begins at index 0, 'b' at index 1, and 'c' at index 2. After index 1 is removed, 'a' remains at index 0 and 'c' moves to index 1.

aindex 0aindex 0bindex 1cindex 1cindex 2stays at 0moves from 2 to 1
How do the remaining elements' indices change after the element at index 1 is removed?

Discarding with del

del also removes a list element by index and produces the same shortened list. If del removes index 1 from ['a', 'b', 'c'], the result is ['a', 'c']. The difference is that del does not return 'b' for later use; it simply discards the removed element.

Because del is a statement rather than an expression that returns a value, it cannot be used as the right-hand side of an assignment. Trying to capture its result, as in assigning del t[1] to a variable, produces a syntax error. This is why del is appropriate when the removed value is not needed.

ToolRemoval targetWhat happens to the removed valueBest choice when
popAn element at a specified indexThe value is returnedYou need to capture or use the value
delAn element at a specified indexThe value is discardedYou only want to remove the element

Both tools modify the list in place, but pop returns the removed element and del does not.

popreturns removed valuecapture valueuse laterdeldiscards removed valuediscard valueno later usechoose whenchoose when
What is the practical difference between pop and del when removing an element by index?

Common Selection Mistakes

  • Common mistake

    Choosing del when the removed value is needed later.

    What it looks like

    A program removes an element and then expects a variable to contain that removed value.

    Why it fails

    del discards the removed value and provides no return value to capture.

    Do this instead

    Choose pop when the program needs to capture or use the removed value.

  • Common mistake

    Assuming later elements keep their original indices.

    What it looks like

    After removing the element at index 1, a program still treats the original element at index 2 as being at index 2.

    Why it fails

    Elements to the right of the removed position shift one place to the left.

    Do this instead

    Reconsider the indices after every removal; the original index 2 becomes index 1 in the three-element example.

  • Common mistake

    Trying to assign the result of del.

    What it looks like

    A learner attempts to capture the result of deleting an indexed element.

    Why it fails

    del is a statement, not an expression that returns a value, so this produces a syntax error.

    Do this instead

    Use pop when a returned value must be assigned to a variable.

  • Common mistake

    Using pop without considering intent.

    What it looks like

    A program uses pop even though it has no use for the removed value.

    Why it fails

    Although using pop in this situation is not wrong, del communicates the discard intention more clearly and is slightly more efficient.

    Do this instead

    Use del when the removed value is simply being discarded.

Practice the Decision

EASY

A list contains three elements. You need to remove the element at index 1, and another part of the program must use the removed value afterward. Which tool should you choose, and what should you expect to happen to the elements that were after index 1?

Hints
  • Choose the tool that returns the removed element.
  • The elements to the right of the removed position shift one index to the left.
EASY

A second list needs to lose its element at index 1, but the removed value has no further use. Which tool better communicates that intention?

Hints
  • This case does not require a return value.
  • The list still changes in place, and later elements still shift left.
Key point

For both practice situations, removing the middle element changes the list and shifts every later element down by one index. The deciding question is whether the removed value must be retained.

Key Takeaways

  1. 1
    pop removes an element by index and returns the removed value.
  2. 2
    del removes an element by index and discards the removed value.
  3. 3
    Both tools modify the list in place.
  4. 4
    Every element to the right of a removed position shifts one index to the left.
  5. 5
    Choose pop when the removed value is needed; choose del when it is not.

At a glance

  • Use pop to remove a list element by index and retain its value.
  • Use del to remove a list element by index when the value should be discarded.
  • Removing an element shifts all later elements one position to the left.
  • The choice between pop and del depends mainly on whether the removed value is needed afterward.

More concepts starting with R