To create a fixed size Queue in Python, you can use two modules, the Queue module and the Collections module. Read the following article for more information.
How to create a fixed size Queue in Python
The queue is an abstract data structure. Queues follow the FIFO rule (first in, first out), which means first in, first out. Hence it is named queue. The queue is similar to real-life queuing.
To achieve this, I have the following ways:
Use Queue module
The ‘queue’ module is built-in in Python. Using the queue module to process the queue is quite fast because it has many built-in functions. You can use the Queue (maxsize) function to initialize a variable with a maximum number of items max size.
Example:
- Import module queue.
- Use the Queue (maxsize =4 ) function to create a queue of up to 4 items.
- Use the put() function to add an element to the queue.
- Use the full() function to check the queue status before adding items.
- Use the get() function to output the items in the queue.
from queue import Queue # Initializing a queue. The full item is 4 myQueue = Queue(maxsize = 4) # Use the put() function to put an item in the queue myQueue.put('google') myQueue.put('learnshareit') myQueue.put('quora') myQueue.put('educative') # Check if the queue is full print('Is the queue full?', myQueue.full()) # Use the get() function to print out the items in the queue print('Items in the queue:') print(myQueue.get()) print(myQueue.get()) print(myQueue.get()) print(myQueue.get())
Output:
Is the queue full? True
Items in the queue:
google
learnshareit
quora
educative
Use Collections module
In Python, the ‘collections’ module is built in. In the collections module, there is a deque class. You can use the deque class to initialize a queue with the ‘maxlen’ argument creating a queue with the maximum number of items.
Example:
- Import module collections.
- Using the deque() function initializes a queue. The function pass argument ‘maxlen’ specifies the maximum number of items in the queue.
- Use the append() function to add an element to the queue.
- If an element is added when the queue is full, the first element will be deleted, and the element will be added to the queue at the end of the queue.
from collections import deque # Initializing a queue. The maximum item is 4 myQueue = deque(maxlen=4) # Use the append() function to add items to the queue myQueue.append('google') myQueue.append('learnshareit') myQueue.append('quora') myQueue.append('educative') print(myQueue) # Add items when the queue is full myQueue.append('e') print(myQueue)
Output:
deque(['google', 'learnshareit', 'quora', 'educative'], maxlen=4)
deque(['learnshareit', 'quora', 'educative', 'e'], maxlen=4)
Summary
Here are the solutions that can help you create a fixed size Queue in Python. If you have any questions about this article, please leave a comment below. I will answer your questions. Thank you for reading!
Maybe you are interested:
- Pass multiple arguments to the map() function in Python
- How to compare values to None in Python
- How To Find difference between two data frames

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java