The pop() method of deque class

Method signature:

pop()

Parameters:

None

Return value:

The removed element from the deque.

Overview:

  • The pop() method removes an element from the right side of a deque and returns the value.
  • The pop() operation is an essential method on a stack which removes the element that came in last.
  • The pop() implements the LIFO on a stack. Since a deque is both a stack and queue, pop() method is provided as part of the deque implementation.
  • Invoking pop() on a empty deque object will raise an exception of type IndexError.

Example:

import collections

 

seriesDeque = collections.deque((10,30,50,70,90))

print("Deque of numbers:")

print(seriesDeque)

 

removedElem = seriesDeque.pop()

 

print("pop removed the number:%d"%(removedElem))

 

print("Deque elements after a pop:")

print(seriesDeque)

 

Output:

Deque of numbers:

deque([10, 30, 50, 70, 90])

pop removed the number:90

Deque elements after a pop:

deque([10, 30, 50, 70])


Copyright 2024 © pythontic.com