CKTIS / Python
list.pop
Removes and returns the selected list element, defaulting to the last one.
Version: Python 3.14; repository image, production runtime not attested
Syntax
a.pop(index=-1, /)Removes and returns the selected list element, defaulting to the last one.
Popping from an empty list raises IndexError even when no index was supplied.
Removes and returns the indexed item; valid negative positions count back from the end. Popping zero returns three, then the default last position returns eight. Any index on an empty list, or an unavailable index on a nonempty list, raises IndexError. This assumes the index fits the platform index type: CPython 3.14 raises OverflowError for 10**100 before consulting list bounds.
a = [1]
try:
a.pop(10**100)
except OverflowError:
assert a == [1]
else:
raise AssertionError
Examples
a = [3, 8]
print(a.pop(0))
print(a.pop())
try:
a.pop()
except IndexError as error:
print(type(error).__name__)
Result
3 8 IndexError
Removes and returns the indexed item; valid negative positions count back from the end. Popping zero returns three, then the default last position returns eight. Any index on an empty list, or an unavailable index on a nonempty list, raises IndexError. This assumes the index fits the platform index type: CPython 3.14 raises OverflowError for 10**100 before consulting list bounds.