list.pop
Remove an item of the list by index and return it.
Python 3.13
list.pop([i])Parameters
- iintoptional
- The index of the item to remove; the last one by default.
Return value
The removed item; raises IndexError for an empty list or an index out of range.
Examples
Removing the last item
Python 3.13
a = [1, 2, 3]
print(a.pop())
print(a)3 [1, 2]
Removing an item by index
Python 3.13
a = ['a', 'b', 'c']
print(a.pop(0))
print(a)a ['b', 'c']
Details
Removing from the end is fast, while a.pop(0) shifts all the other items, so for a queue collections.deque with its popleft method is better. A negative index counts from the end: a.pop(-2) removes the second-to-last item.
