Explain Codes LogoExplain Codes Logo

Difference between del, remove, and pop on lists

python
list-manipulation
python-basics
best-practices
Nikita BarsukovbyNikita Barsukov·Jan 2, 2025
TLDR

In Python lists, del erases by index, remove() eliminates first found value, and pop() extracts an item by index and hands it back. Quick demo:

my_list = ['a', 'b', 'c', 'b'] del my_list[1] # ['a', 'c', 'b'], 'b' didn't notice it was missing a neighbor my_list.remove('b') # ['a', 'c'], 'b' has officially left the building item = my_list.pop() # item: 'c', list: ['a'], 'c' takes the stage (aka returns)!

A fast snapshot of the trio, but the devil is in the details.

Making the right choice when deleting

The pluck by value: remove()

If the value you want gone is known, not the index, you remove() it!

  • Pros: Need value knowledge only, useful when indices are a mystery.
  • Cons: Removes first value match, might be a bit slowpoke (O(n), sweetie).

But beware! If the value isn't on the list, remove() throws a mean ValueError.

The slicer-dicer: del

If indexes or slices are your game, del is your tool.

  • Pros: Handy for many tasks; can eliminate single items, slice through a list or obliterate all.
  • Cons: Doesn't return the deleted item, index or slice knowledge required.

Mind you! Using del unwisely might lead to unexpected list mutations or an IndexError.

The courteous extractor: pop()

Choose pop() when you erase and need the deleted item for a curtain call.

  • Pros: Deletes, returns item, defaults to the last if index is not mentioned.
  • Cons: Exclusively for single items, index required.

Just like del, pop() will produce an IndexError if your index is out on vacation.

Power tips and tricks (aka "I wish I knew that before")

Deleting without knowing indexes

Get extra points using filter or list comprehension with remove() for a mass wipeout without index knowledge.

Slicing like a pro with del

del wields the slice like a sword. To keep only the first half of the list:

del my_list[len(my_list)//2:] # Like a magic trick for the second half

Return values are always in style

When re-using the removed player, pop() wins over del and remove().

Error handling, the life-saver

Use try-except blocks to sail smoothly past removal errors and avoid sinking your code.

Performance and safety? Yes, please!

Deletion and time efficiency

Shed a light on computational overhead, behold: pop() and del do it in O(n - i), remove() in O(n).

Safe methods to the rescue

Always check indices and existence prior to deletion to elude unwanted exceptional surprises.

Cleanliness is next to godliness

Sometimes, wiping a list clean triumphs over one-item-at-a-time deletion, for performance and readability:

my_list.clear() # All items gone, leaves a pristine empty list

Walking on eggshells with del

Take heed! While del can free memory by deleting the entire list, watch out for unintended side effects:

del my_list[:] # Empties the list but list instance is hanging around
del my_list # List variable has left the memory building

Pro tip: To keep code integrity, use .clear() or slicing over full list deletion with del.