Free Ebook cover System creation course with Python and Django complete

System creation course with Python and Django complete

New course

176 pages

Data Structures in Python: Queues

Capítulo 20

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

The correct answer is 3. A queue is a data structure following the FIFO (First In First Out) principle. In Python, queues can be implemented using lists, collections.deque, or the queue.Queue class, which are designed to handle the queue operations efficiently.

Next chapter

Data Structures in Python: Trees

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.