The remove() method of deque class in python collections module

The remove() methods removes the first occurence of a value specified by the parameter value. If the value is not found remove() method raises an IndexError.

Deque Updated 20 Mar 2017 1 min read

Method Signature

remove(value)

Return value

Returns the removed element.

Method Overview

  • The remove() method removes the first occurrence of the value from a deque as specified the value parameter.
  • If the deque object does not have any element with the specified value, remove() raises an exception of type ValueError.

Example

python
import collections

tokens          = ("I", "chatter", "chatter", "as", "I", "flow")

poetryDeque     = collections.deque(tokens)

removeValue     = "chatter"

print("Deque entries before remove():")

print(poetryDeque)

poetryDeque.remove(removeValue)

print("Deque entries after removing the entry:%s"%(removeValue))

print(poetryDeque)

Output

Output
Deque entries before remove():

deque(['I', 'chatter', 'chatter', 'as', 'I', 'flow'])

Deque entries after removing the entry:chatter

deque(['I', 'chatter', 'as', 'I', 'flow'])