remove
list.pop
Removes and returns the item at position i (default -1, the last item). Raises IndexError on an empty list or an out-of-range index.
list.pop([i]) Parameters
| Parameter | Purpose |
|---|---|
| i (optional) | Index to pop; defaults to -1 (last element). Accepts negative indices. |
| return value | Unlike most mutators, pop returns the removed element — assignable. |
Examples
>>> lst = [1, 2, 3]
>>> lst.pop()
3
>>> lst
[1, 2] Default pops and returns the last element.
>>> lst = [1, 2, 3]
>>> lst.pop(0)
1
>>> lst
[2, 3] pop(0) is O(n) — use collections.deque.popleft() for queues.
>>> [].pop()
IndexError: pop from empty list Empty list raises IndexError.
>>> lst = [1, 2, 3]
>>> lst.pop(-2)
2 Negative indices allowed.
Gotcha
pop() is the one common list mutator that RETURNS the removed value (not None). pop(0) is O(n); prefer collections.deque for FIFO patterns.
Related methods
list.remove
Removes the first occurrence of a value equal to x from the list. Raises ValueError if x is not present; returns None.
del list[...]
Statement that deletes the element or slice at the given position, mutating the list in place. Unlike pop() it does not return the removed value; unlike remove() it targets by index, not by value.
list.clear
Removes all items from the list in place. Equivalent to `del lst[:]`; returns None.
← All Python list methods · Dict methods · list vs tuple vs set