Queue in Python Show Improve Article Save Article Like Article
Improve Article Save Article 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:
ImplementationThere are various ways to implement a queue in Python. This article covers the implementation of queue using data structures and modules from Python library.
Implementation using listList 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. Python3
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list0 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list1 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list2
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list4 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list5 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list6 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list5 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list9 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])2 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7
Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])7 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque0
Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque2 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque5 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque8 Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque9 0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False0 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque8 Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque9 0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False0 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque8 Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque9 0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False0
Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 # Python program to 2Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque0
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, in print(queue.pop(0)) IndexError: pop from empty list
Implementation using collections.dequeQueue 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. Python3
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list1 # using list 3
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list6 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list9 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7
Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])2 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7
Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])7 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 # Initializing a queue 1
Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 # Initializing a queue 6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 # Initializing a queue 9Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 # Initializing a queue 9Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 # Initializing a queue 9
Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 # Python program to 2Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 # Initializing a queue 1
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list12 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list13 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list14 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, in q.popleft() IndexError: pop from an empty deque
Implementation using queue.QueueQueue 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.
Python3
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list16 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list17
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list0 # demonstrate queue implementation 7 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list23
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list1 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list28 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list1 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list30 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list33 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list34 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list36
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list38 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list39 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list6 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list39 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list9 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list39 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])2 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list49 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list50 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list53 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list54
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list56 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 # Initializing a queue 6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list62 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list62 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list62
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list68 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list50 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list72 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list73
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list39 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list76 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list7 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list72 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list81 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])5 Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])6 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list84 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list85
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list87 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list88 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list89 Output: 0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False
Recommended Solve DSA problems on GfG Practice. Solve Problems My Personal Notes arrow_drop_up Save Please Login to comment...Apakah queue dapat diimplementasikan menggunakan array?Operasi pada queue dapat diimplementasikan dengan menggunakan array, structure (record) dan pointer.
Bagaimana cara kerja queue?Queue adalah struktur data linier yang menerapkan prinsip operasi dimana elemen data yang masuk pertama akan keluar lebih dulu. Prinsip ini dikenal dengan istilah FIFO (First In, First Out).
Apa itu struktur data queue?Queue atau antrian adalah suatu kumpulan data yang penambahan elemennya hanya bisa dilakukan pada suatu ujung (disebut dengan sisi belakang atau rear), dan penghapusan atau pengambilan elemen dilakukan lewat ujung yang lain (disebut dengan sisi depan atau front).
Append python buat apa?Append. Salah satu fitur dalam array python yang cukup sering digunakan adalah fungsi append. Fungsi append ini berguna untuk menambahkan nilai array pada urutan terakhir. Fungsi ini sedikit berbeda dengan fungsi insert, dimana fungsi insert bisa menambahkan nilai array pada posisi tertentu.
|