| An Introduction to Python by Guido van Rossum and Fred L. Drake, Jr. Paperback (6"x9"), 124 pages ISBN 0954161769 RRP £12.95 ($19.95) Sales of this book support the Python Software Foundation! Get a printed copy>>> |
5.1.1 Using Lists as Stacks
The list methods make it very easy to use a list as a stack, where the
last element added is the first element retrieved ("last-in,
first-out"). To add an item to the top of the stack, use
append(). To retrieve an item from the top of the stack, use
pop() without an explicit index. For example:
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
| ISBN 0954161769 | An Introduction to Python | See the print edition |