The pop() method of deque class

The pop() method of deque class removes the last element of a deque object and returns the value. Calling pop() on an empty deque instance raises an IndexError.

Deque Updated 20 Mar 2017 1 min read

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 Last In First Out - 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

python
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

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])