Warning: session_start(): open(/tmp/sess_1fe87705304055e8bf8205e0e653432b, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Clear All Items From A Queue In Python - LearnShareIT

How To Clear All Items From A Queue In Python

Clear all items from a Queue in Python

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:

Leave a Reply

Your email address will not be published. Required fields are marked *