| 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.2 Using Lists as Queues
You can also use a list conveniently as a queue, where the first
element added is the first element retrieved ("first-in,
first-out"). To add an item to the back of the queue, use
append(). To retrieve an item from the front of the queue,
use pop() with 0 as the index. For example:
>>> queue = ["Eric", "John", "Michael"]
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.pop(0)
'Eric'
>>> queue.pop(0)
'John'
>>> queue
['Michael', 'Terry', 'Graham']
| ISBN 0954161769 | An Introduction to Python | See the print edition |