The methods append
and pop
make it very easy to use a list
to implement the data structure known as a stack:
>>> stack = []
>>> stack.append(1)
>>> stack.append(2)
>>> stack.append(3)
>>> stack.append(4)
>>> print(stack)
[1, 2, 3, 4]
>>> stack.pop()
4
>>> print(stack)
[1, 2, 3]
The end of the list is the top of the stack from which items may be added or removed ("last in, first out" (LIFO): think of a stack of dinner plates).