# Queue in Python

Like stack, queue is a linear data structure that stores items in First In First Out (FIFO) manner. With a queue the least recently added item is removed first. A good example of queue is any queue of consumers for a resource where the consumer that came first is served first.

Operations associated with queue are:

**Enqueue:**Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition – Time Complexity : O(1)**Dequeue:**Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition – Time Complexity : O(1)**Front:**Get the front item from queue – Time Complexity : O(1)**Rear:**Get the last item from queue – Time Complexity : O(1)

## Implementation

There are various ways to implement a queue in Python. This article covers the implementation of queue using data structures and modules from Python library.

Queue in Python can be implemented by the following ways:

- list
- collections.deque
- queue.Queue

#### Implementation using list

`List `

is a Python’s built-in data structure that can be used as a queue. Instead of `enqueue()`

and `dequeue()`

, `append()`

and `pop()`

function is used. However, lists are quite slow for this purpose because inserting or deleting an element at the beginning requires shifting all of the other elements by one, requiring O(n) time.

`# Python program to ` `# demonstrate queue implementation ` `# using list ` ` ` `# Initializing a queue ` `queue ` `=` `[] ` ` ` `# Adding elements to the queue ` `queue.append(` `'a'` `) ` `queue.append(` `'b'` `) ` `queue.append(` `'c'` `) ` ` ` `print` `(` `"Initial queue"` `) ` `print` `(queue) ` ` ` `# Removing elements from the queue ` `print` `(` `"\nElements dequeued from queue"` `) ` `print` `(queue.pop(` `0` `)) ` `print` `(queue.pop(` `0` `)) ` `print` `(queue.pop(` `0` `)) ` ` ` `print` `(` `"\nQueue after removing elements"` `) ` `print` `(queue) ` ` ` `# Uncommenting print(queue.pop(0)) ` `# will raise and IndexError ` `# as the queue is now empty ` |

*chevron_right*

*filter_none*

**Output:**

Initial queue ['a', 'b', 'c'] Elements dequeued from queue a b c Queue after removing elements []

Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, inprint(queue.pop(0)) IndexError: pop from empty list

#### Implementation using collections.deque

Queue in Python can be implemented using `deque`

class from the collections module. Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity. Instead of `enqueue `

and `deque`

, `append() `

and `popleft()`

functions are used.

`# Python program to ` `# demonstrate queue implementation ` `# using collections.dequeue ` ` ` ` ` `from` `collections ` `import` `deque ` ` ` `# Initializing a queue ` `q ` `=` `deque() ` ` ` `# Adding elements to a queue ` `q.append(` `'a'` `) ` `q.append(` `'b'` `) ` `q.append(` `'c'` `) ` ` ` `print` `(` `"Initial queue"` `) ` `print` `(q) ` ` ` `# Removing elements from a queue ` `print` `(` `"\nElements dequeued from the queue"` `) ` `print` `(q.popleft()) ` `print` `(q.popleft()) ` `print` `(q.popleft()) ` ` ` `print` `(` `"\nQueue after removing elements"` `) ` `print` `(q) ` ` ` `# Uncommenting q.popleft() ` `# will raise an IndexError ` `# as queue is now empty ` |

*chevron_right*

*filter_none*

**Output:**

Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])

Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, inq.popleft() IndexError: pop from an empty deque

#### Implementation using queue.Queue

Queue is built-in module of Python which is used to implement a queue. `queue.Queue(maxsize)`

initializes a variable to a maximum size of maxsize. A maxsize of zero ‘0’ means a infinite queue. This Queue follows FIFO rule.

There are various functions available in this module:

**maxsize**– Number of items allowed in the queue.**empty()**– Return True if the queue is empty, False otherwise.**full()**– Return True if there are maxsize items in the queue. If the queue was initialized with maxsize=0 (the default), then full() never returns True.**get()**– Remove and return an item from the queue. If queue is empty, wait until an item is available.**get_nowait()**– Return an item if one is immediately available, else raise QueueEmpty.**put(item)**– Put an item into the queue. If the queue is full, wait until a free slot is available before adding the item.**put_nowait(item)**– Put an item into the queue without blocking.**qsize()**– Return the number of items in the queue. If no free slot is immediately available, raise QueueFull.

`# Python program to ` `# demonstrate implementation of ` `# queue using queue module ` ` ` ` ` `from` `queue ` `import` `Queue ` ` ` `# Initializing a queue ` `q ` `=` `Queue(maxsize ` `=` `3` `) ` ` ` `# qsize() give the maxsize ` `# of the Queue ` `print` `(q.qsize()) ` ` ` `# Adding of element to queue ` `q.put(` `'a'` `) ` `q.put(` `'b'` `) ` `q.put(` `'c'` `) ` ` ` `# Return Boolean for Full ` `# Queue ` `print` `(` `"\nFull: "` `, q.full()) ` ` ` `# Removing element from queue ` `print` `(` `"\nElements dequeued from the queue"` `) ` `print` `(q.get()) ` `print` `(q.get()) ` `print` `(q.get()) ` ` ` `# Return Boolean for Empty ` `# Queue ` `print` `(` `"\nEmpty: "` `, q.empty()) ` ` ` `q.put(` `1` `) ` `print` `(` `"\nEmpty: "` `, q.empty()) ` `print` `(` `"Full: "` `, q.full()) ` ` ` `# This would result into Infinite ` `# Loop as the Queue is empty. ` `# print(q.get()) ` |

*chevron_right*

*filter_none*

**Output:**

0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False

## Recommended Posts:

- Stack and Queue in Python using queue Module
- Priority Queue in Python
- Heap queue (or heapq) in Python
- Python | Queue using Doubly Linked List
- Check if a queue can be sorted into another queue using a stack
- Reversing a Queue using another Queue
- Queue using Stacks
- Reversing a Queue
- Sharing a queue among three threads
- Applications of Priority Queue
- Reverse a path in BST using queue
- Reversing a queue using recursion
- Priority Queue | Set 1 (Introduction)
- Queue Interface In Java
- Sort the Queue using Recursion

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.