Skip to main content

How to Delete Items from a List in Python with del

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Sometimes you know where the items you don't want are: the second one, or everything after the first two. The del statement removes items by position, one at a time or a whole range with a slice. This guide trims a packing list twice, with real output from Python 3.15.2.

Syntax​

Syntax
del <list>[<index>]
del <list>[<start>:<stop>]
PartWhat it means
<list>[<index>]One item, by its position. The first item is 0, the last is -1
<list>[<start>:<stop>]A slice: the items from <start> up to, not including, <stop>. Leave <stop> out to go to the end

Python's tutorial: "There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or clear the entire list".

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

del_items.py
items = ["tent", "stove", "lantern", "map", "rope"]

del items[1]
print(items)

del items[2:]
print(items)

The del_items.py script in a macOS-style code window: a packing list of tent, stove, lantern, map and rope, then del items[1] and a print, then del items[2:] and another print.

  • del items[1] removes the item at position 1, "stove".
  • del items[2:] removes everything from position 2 to the end of what's left.

Step 2: Run It​

Terminal
uv run python del_items.py
Output
['tent', 'lantern', 'map', 'rope']
['tent', 'lantern']

The output in a terminal: the list without stove, then only tent and lantern.

The first del took out "stove", and the items after it moved up a place. The slice then removed "map" and "rope", the items from position 2 on. Both changed the same list: del doesn't make a copy.

Common Mistakes​

MistakeWhat happensDo this instead
A position past the end, del items[10]IndexError: list assignment index out of rangeCheck len(items) first
del items without bracketsIt deletes the variable, not its items: the next print(items) gives NameError: name 'items' is not defined. Did you mean: 'iter'?Use del items[:] to empty the list
Keeping the deleted item, removed = del items[0]SyntaxError: invalid syntax: del is a statement and gives nothing backUse items.pop(0), which returns the item

Troubleshooting​

SymptomLikely causeFix
The wrong item was deletedPositions start at 0, so items[1] is the second itemCount from zero

FAQ​

Does a slice past the end fail too?​

No. del x[5:] on a two-item list removed nothing and raised no error: slices stop at the end of the list.

How do I delete the last item?​

del items[-1]: negative positions count from the end. On ['a', 'b', 'c', 'd'] it left ['a', 'b', 'c'].

Conclusion​

del items[1] removed "stove" and del items[2:] removed the rest after "lantern", both in the same list.

Additional Resources​