How to implement a queue with maximum length in Python

In some applications, you might need to implement a queue that starts off empty, but you want it to grow and be limited to a certain length. Such a queue with a maximum length can be implemented easily using deque:

Tried with: Python 2.7.3

How to use queue in Python

The ubiquitous list can be used as a queue in Python. But, that is not efficient because of the way lists are implemented.

Using deque from the collections module is a straightforward way to use queues:

Tried with: Python 2.7.3

Check queue of a network printer in Windows

Queue of a network printer

I did not know that you could check the queue of documents that are waiting to be printed on a network printer. This is assuming that the computer is using Windows 7 and the printer is on a Windows network.

From Start, open Devices and Printers and your network printer should be listed here. Right-click on it and choose See what’s printing. The dialog that pops up shows the current document printing on this printer and also any other documents queued up to be printed on it.

Tried with: Windows 7 64-bit

Using list as queue in Python

The humble list can be used as a simple queue in Python.

q = []

for i in range( 0, 5 ):
    q.append( i ) # Add to back of queue

while q: # Queue not empty
    j = q.pop( 0 ) # Get from front of queue
    print( j )

# Output: 0 1 2 3 4

Note that popping the first item of a list is not optimal due to the way it is implemented. For large queues, using deque as queue is a much better option.

Tried with: Python 3.2.2