Today’s topic I want to introduce to you is how to clear all items from a Queue in Python. I use the clear() method to clear all items or the get() method (but for the get() method you should only use a few items in the queue). Please read the following article.
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.
Clear all items from a Queue in Python
Use the clear() method
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 clear method to delete 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('visit') myQueue.put('learnshareit') myQueue.put('website') myQueue.put('!!!') # Check if the queue is full print(myQueue.queue) # Using the clear() method deletes the elements in the queue. myQueue.queue.clear() print('Queue after removing all elements:', myQueue.queue)
Output:
deque(['visit', 'learnshareit', 'website', '!!!'])
Queue after removing all elements: deque([])
Use the get()method
You can also use the get method to delete all items from the queue, but the method deletes items one by one, so if your queue has many elements, this is not the most optimal way because you have to delete it manually.
Example:
- Import module queue.
- Use the Queue (maxsize =2 ) function to create a queue of up to 2 items.
- Use the put() function to add an element to the queue.
- Use the get() method to delete items in the queue.
- Note: This method should only be used for queues with few items.
import queue # Initializing a queue. The full item is 2. myQueue = queue.LifoQueue(2) # Use the put() function to put an item in the queue myQueue.put('visit') myQueue.put('Learnshareit') # Using the get() method deletes the elements in the queue. deleteItems = myQueue.get() deleteItems1 = myQueue.get() print('Item is removed from the queue:', deleteItems) print('Item is removed from the queue:', deleteItems1) # Check if the queue is full print(myQueue.full())
Output:
Item is removed from the queue: Learnshareit
Item is removed from the queue: visit
False
In the above example items have been removed from the queue by the get() method. I use the full() method to double-check that the correct items are deleted. The full() method returns False, then the items are deleted completely.
Summary
You should see that the above method works for you to clear all items from a Queue in Python. Using the clear() method is probably the least complicated you can use this way. Thanks for reading the article, if you have any questions please leave a comment below.
Maybe you are interested:
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