Concepts / Removing Elements by Value with remove

Removing Elements by Value with remove

pop removes an element by index and returns it; del removes an element by index without returning it.

  • Programming

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.

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.

pop(1)returned valuet['a', 'b', 'c']t['a', 'c']bindex 1x'b'
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.

stays at 0moves from 2 to 1aindex 0aindex 0bindex 1cindex 1cindex 2
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
choose whenchoose whenpopreturns removed valuecapture valueuse laterdeldiscards removed valuediscard valueno later use
What is the practical difference between pop and del when removing an element by index?

Common Selection Mistakes

  • Choosing del when the removed value is needed later.

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

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

  • Assuming later elements keep their original indices.

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

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

  • Trying to assign the result of del.

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

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

  • Using pop without considering intent.

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

    Fix: 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.

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

Key Takeaways

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