Solution preview: Build a filtered list with a list comprehension for condition-based removals, use pop()
or del
to remove by index, and use remove()
to delete the first matching value. Python lists are dynamic arrays, so index removals shift trailing elements; prefer a single-pass filter when removing many items by condition.
Method 1 — Remove items by condition (single pass)
This approach builds a new list that keeps only the elements you want. It runs in linear time and avoids the pitfalls of modifying a list while iterating over it. See list comprehensions in the Python docs: docs.python.org.
Step 1: Write a list comprehension that keeps elements which satisfy your condition.
nums = [1, 2, 5, 4, 3, 5, 6]
filtered = [x for x in nums if x != 5]
Step 2: Replace the original list if you need to modify it in place.
nums[:] = [x for x in nums if x != 5]
Step 3: For nested lists, use a nested comprehension to filter inner sequences.
lists = [[1, 2, 3], [4, 5, 6]]
lists = [[x for x in inner if x % 2 == 0] for inner in lists]
Why this works better: you make one pass over the data and avoid O(n²) patterns that happen when deleting from the middle repeatedly.
Method 2 — Remove by index with pop or del
Use an index when you know the position. pop()
returns the removed item, while del
does not. See the official docs for list.pop and the del statement.
Step 1: Remove and get the item at an index with pop()
.
items = ["a", "b", "c", "d"]
removed = items.pop(2) # removes "c"
Step 2: Remove at an index without returning the value using del
.
items = ["a", "b", "c", "d"]
del items[1] # removes "b"
Step 3: Delete a slice (range of indices) with del
slice syntax.
items = ["a", "b", "c", "d", "e"]
del items[1:4] # removes "b", "c", "d"
Notes: Index-based removals from the middle shift all following elements left. Negative indices work (-1
is the last item). Calling pop()
without an index removes the last item.
Method 3 — Remove the first matching value with remove
Use this when you know the value to delete and only need to drop its first occurrence. See list.remove.
Step 1: Call remove(value)
to delete the first match.
names = ["Ada", "Linus", "Ada", "Guido"]
names.remove("Ada") # removes the first "Ada"
Step 2: Guard against missing values to avoid ValueError
.
if "Grace" in names:
names.remove("Grace")
Tip: If you need to remove all occurrences of a value, use Method 1 to filter instead of looping with repeated remove()
.
Method 4 — Clear the entire list
When you want to empty a list but keep the variable, use clear()
. See list.clear.
Step 1: Empty the list in place.
records = [1, 2, 3]
records.clear() # records becomes []
Alternative: assignment to an empty slice (records[:]=[]
) also clears in place.
Method 5 — Safety and performance tips
These guardrails help you avoid skipped elements and slow operations when removing items.
Step 1: Avoid modifying a list while iterating over it.
# Risky: may skip elements
for x in items:
if should_delete(x):
items.remove(x) # don't do this
# Safe: build a filtered list (Method 1)
items = [x for x in items if not should_delete(x)]
Step 2: Prefer a single-pass filter when removing many items.
# Efficient for many deletions
data[:] = [x for x in data if keep(x)]
Step 3: Use index-based removal for a known position or the last element.
# Fast path for the end
last = data.pop() # remove last item
Why this matters: list deletions from the middle shift elements, so repeated in-loop deletions add up. A single pass with a comprehension keeps the work proportional to the list size.
Quick reference
Learn when to use each tool.
[x for x in seq if keep(x)]
: remove by condition; single pass; safest for multiple removals.seq.pop(i)
: remove by index and return the item (defaults to last element).del seq[i]
: remove by index without returning; also supports slices.seq.remove(value)
: remove first matching value; raises if not found.seq.clear()
: remove all items; keeps the list object.
That’s it—pick the method that fits your case: filter for conditions, index for positions, and value-based removal when you only need the first match.
Member discussion