Data Structures in Python: Queue

Satoro
2 min readOct 18, 2021

--

The above figure shows a queue of people waiting to get the item. So, who do you think will get the item first? Of course, the one who came first. That means the person who came first will leave first.

Now, what if I told you this represents a data structure and this data structure is called a queue. Queue follows the pattern of FIFO(First In First Out) .i.e. the first element added in the queue is the first element removed.

You may think like the previous tutorial we can use a list in this also because we have an append method to add elements and using index we can remove the first element. But no, that's not an efficient method to do it because whenever any item is removed from the list every item after that needs to shift back to fill the gap created by removing that item. In this case, if we remove the first item then every other item will be shifted once backward which will make it too slow when executed. That's why we don’t use the list as a queue.

In Python collections module provides ‘deque’ which is fast and efficient for faster append and pops from both ends. Its execution is too simple:

from collections import dequequeue = deque([4, 6, 8])
queue.append(10)
print(queue)#OUTPUT
deque([4, 6, 8, 10])

Now we have to remove the element and for that, we would use ‘popleft’ method:

queue.popleft()print(queue)#OUTPUT
deque([6, 8, 10])

This will be enough for queue using deque in Python. We can create our own Queue class and create append and pop methods which I will show you tomorrow. See you and have a good day!

Lectures

Lecture 1: https://medium.com/@saifmdco/data-structures-with-python-introduction-4dadeffa2215

Lecture 2: https://medium.com/@saifmdco/data-structures-with-python-arrays-c498518bf7fd

Lecture 3: https://medium.com/@saifmdco/data-structures-in-python-lists-653a3ad103ab

Lecture 4: https://medium.com/@saifmdco/data-structures-in-python-tuples-3c350640cd9a

Lecture 5: https://medium.com/@saifmdco/data-structures-in-python-dictionary-fad27ffdda8b

Lecture 6: https://medium.com/@saifmdco/data-structures-in-python-2d-array-6bc0154aa717

Lecture 7: https://medium.com/@saifmdco/data-structures-in-python-matrix-22adba5aa597

Lecture 8: https://medium.com/@saifmdco/data-structures-in-python-sets-92d5445e97e8

Lecture 9: https://medium.com/@saifmdco/data-structures-in-python-chainmap-fca2a9d47249

Lecture 10: https://medium.com/@saifmdco/data-structures-in-python-linkedlist-50f2118c659e

Lecture 11: https://medium.com/@saifmdco/data-structures-in-python-stack-6bf182d63581

Lecture 12: https://medium.com/@saifmdco/data-structures-in-python-queue-6361d3dcff0

Lecture 13: https://medium.com/@saifmdco/data-structures-in-python-dequeue-1a585e269a55

Lecture 14: https://medium.com/@saifmdco/data-structures-in-python-custom-stack-b7b9173b4eae

Lecture 15: https://medium.com/@saifmdco/data-structures-in-python-hash-table-39be784eefc1

Lecture 16: https://medium.com/@saifmdco/data-structures-in-python-doubly-linked-list-fe698d74756c

--

--