Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. del vs remove vs pop

Python · Collections & Basics

del vs remove vs pop

Easypython-07
listdelremovepop

Question

What is the difference between del, remove(), and pop() when working with lists?

Solution

These three ways of deleting from a list answer different questions: by index, by value, or "remove and return."

| Tool | What you specify | Returns value? | Raises if missing | | --- | --- | --- | --- | | del lst[i] | index (or slice) | No | IndexError | | lst.remove(x) | first matching value | No | ValueError | | lst.pop(i=-1) | index (default last) | Yes | IndexError |

nums = [10, 20, 30, 20]

del nums[1]          # remove index 1 → [10, 30, 20]
nums.remove(20)      # remove first 20 → [10, 30]
last = nums.pop()    # remove and return last → last=30, nums=[10]
first = nums.pop(0)  # remove index 0 → first=10, nums=[]

# Slice delete
data = [0, 1, 2, 3, 4]
del data[1:3]        # [0, 3, 4]

# Dict variants
d = {"a": 1, "b": 2}
del d["a"]
val = d.pop("b")     # returns 2
val2 = d.pop("z", None)  # default avoids KeyError

When to use which

  • Know the index → del or pop.
  • Need the removed value → pop.
  • Know the value, not the index → remove.
  • Clearing many indices → slice del or rebuild the list.

🎯 Put this concept into practice

Solidify this answer with real hands-on interview drills in the browser studio.

Open related drill →
PreviousNext