Python list pop() method
Remove an item by position and return it.
Syntax
list.pop(index=-1)
With no argument it removes the last item, so append and pop together make a stack.
Parameters
| index | the position (default: the last) |
Return value
the removed item
Example
Code
stack = [1, 2, 3] top = stack.pop() print(top, stack) queue = ["Sita", "Leo", "Mia"] first = queue.pop(0) print(first, queue)
Output
3 [1, 2] Sita ['Leo', 'Mia']
Good to know.
pop on an empty list raises IndexError.Related
Learn it step by step in the free lesson Lists & Tuples, with exercises that check your answers.