# 4.12. Implementing a Queue in Python¶

It is again appropriate to create a new class for the implementation of the abstract data type queue. As before, we will use the power and simplicity of the list collection to build the internal representation of the queue.

We need to decide which end of the list to use as the rear and which to use as the front. The implementation shown in Listing 1 assumes that the rear is at position 0 in the list. This allows us to use the insert function on lists to add new elements to the rear of the queue. The pop operation can be used to remove the front element (the last element of the list). Recall that this also means that enqueue will be $$O(n)$$ and dequeue will be $$O(1)$$.

Listing 1

class Queue:
"""Queue implementation as a list"""

def __init__(self):
"""Create new queue"""
self._items = []

def is_empty(self):
"""Check if the queue is empty"""
return not bool(self._items)

def enqueue(self, item):
"""Add an item to the queue"""
self._items.insert(0, item)

def dequeue(self):
"""Remove an item from the queue"""
return self._items.pop()

def size(self):
"""Get the number of items in the queue"""
return len(self._items)


CodeLens 1 shows the Queue class in action as we perform the sequence of operations from Table 1.

Activity: CodeLens Example Queue Operations (ququeuetest)

Further manipulation of this queue would give the following results:

>>> q.size()
3
>>> q.is_empty()
False
>>> q.enqueue(8.4)
>>> q.dequeue()
4
>>> q.dequeue()
'dog'
>>> q.size()
2


Self Check