Popleft() Example In Python
In this tutorial, I will introduce you to popleft() method of the Collection module in Python. After this small and easy tutorial, you will understand this function and some others too.
Introduction To Dequeue
Dequeue is nothing but an extended version of simple queue data structures. It is a double-ended queue to append or delete an element at both ends.
In the collections module, the dequeue is already built. So, you don’t need to create the whole dequeue to understand the methods of dequeue. In this tutorial, I will introduce you to methods of dequeue.
Methods of the Dequeue:
- append() : To append an element at the right end.
- appendleft() : To append at the left end.
- pop() : To delete an element at the right end.
- popleft() : To delete an element at the left end.
Dequeue Program In Python
To perform this task you need a collections module that is built-in no need to install it. Just import the module and perform.
We added requirement comments with every piece of code for your better understanding.
import collections # Create dequeue with element 1,3,5,7,9 dequeue = collections.deque([1,3,5,7,9]) #adding one more element in dequeue dequeue.append(11) # print list of the queue with element 1,3,5,7,9,11 print(dequeue) # delete element from right side dequeue.pop() # delete element from left side dequeue.popleft() # print list after deleting two elements print(dequeue)
Output :
[1,3,5,7,9,11] #after deleting the element [3,5,7,9]
Leave a Reply