Python 3 reference · List Methods
Python List pop() Method
Remove and return the item at a position, the last item by default. Raises IndexError on an empty list.
Python 3 Since 3.0
Syntax
list.pop(index=-1)
Parameters
| Parameter | Description |
|---|---|
index optional |
Position of the item to remove. Default: -1 |
Return value
The removed item.
Examples
stack = [1, 2, 3]
print(stack.pop(), stack)
print(stack.pop(0), stack)
Output
3 [1, 2] 1 [2]
todo = ["write", "test", "ship"]
while todo:
print("doing", todo.pop(0))
Output
doing write doing test doing ship
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
list.append()· Add one item to the end of a list, changing the list in place. Returns None.list.remove()· Remove the first item equal to a value. Raises ValueError if the value is not in the list.list.insert()· Insert an item at a given position, shifting later items to the right. Index 0 adds to the front.dict.pop()· Remove a key and return its value. Returns the default if given and the key is missing; otherwise raises KeyError.- Python Lists · tutorial