clear() method of deque class in Python

The clear() method of deque class in Python removes all the elements of a deque object. Number of elements of the deque object after calling clear is zero.

Deque Updated 19 Mar 2017 1 min read

Method Name

clear

Method Signature

clear

Method Overview

  • clear() method of deque class in python removes all the elements of a deque object.

Example

python
import collections

evenNumbers = (2,4,6,8)

evenDeque   = collections.deque(evenNumbers)

print("Deque object with even numbers less than 10:")

print(evenDeque)

print("Deque length:%d"%(len(evenDeque)))

evenDeque.clear()

print("Deque object after calling clear() method:")

print(evenDeque)

print("Deque length:%d"%(len(evenDeque)))

Output

Output
Deque object with even numbers less than 10:

deque([2, 4, 6, 8])

Deque length:4

Deque object after calling clear() method:

deque([])

Deque length:0