5.6. Python Data Structures: Queues

Data structure is a particular way of storing and organizing data on a computer so that it can be used efficiently. In Python, one of the most used data structures is the queue. The queue is a linear data structure that follows the order of operation FIFO (First In First Out), which means that the first element inserted in the queue will be the first to be removed.

Implementing queues in Python

In Python, queues can be implemented in many ways, such as lists, collections.deque, and the queue.Queue class from the queue module.

1. Queues using lists

The simplest way to implement a queue in Python is using a list. However, lists are not efficient for this purpose. While append and pop() lists are fast, inserting or removing elements from the beginning of a list is slow (because all other elements have to be shifted).

# Creating a queue
queue = []

# Adding elements to the queue
queue.append('a')
queue.append('b')
queue.append('c')

print("Initial queue", queue)

# Removing elements from the queue
print("\nElements dequeued")
print(queue.pop(0))
print(queue.pop(0))

print("\nQueue after removing elements", queue)

2. Queues using collections.deque

Queues in Python can be implemented more efficiently using the collections.deque class, which is designed to have quick additions and removals from both ends.

from collections import deque

# Creating a queue
q = deque()

# Adding elements to the queue
q.append('a')
q.append('b')
q.append('c')

print("Initial queue", q)

# Removing elements from the queue
print("\nElements dequeued")
print(q.popleft())
print(q.popleft())

print("\nQueue after removing elements", q)

3. Queues using queue.Queue

The Queue class of the queue module is especially useful in multithreaded programming, where Python objects, which are normally not thread-safe, are manipulated. The Queue class is thread safe and can be used to exchange data between multiple threads.

from queue import queue

# Creating a queue
q = Queue()

# Adding elements to the queue
q.put('a')
q.put('b')
q.put('c')

print("Initial queue", q)

# Removing elements from the queue
print("\nElements dequeued")
print(q.get())
print(q.get())

print("\nQueue after removing elements", q)

In summary, queues are a useful tool in Python for storing and manipulating data in an organized and efficient way. The choice of implementation depends on the specific needs of your program, but it's important to understand how each one works to make the best choice.

Now answer the exercise about the content:

Which data structure in Python follows the FIFO (First In First Out) order of operation?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Data Structures in Python: Trees

Next page of the Free Ebook:

21Data Structures in Python: Trees

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text