Python remove()

method6590595471's avatar
Published Oct 22, 2025
Contribute to Docs

The remove() method removes the first occurrence of a specified value from a deque. If the value is not found in the deque, a ValueError is raised. The method modifies the deque in-place and does not return a value.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

deque.remove(value)

Parameters:

  • value: The item to be removed from the deque. Only the first occurrence is removed.

Return value:

The remove() method returns None.

Exceptions:

  • ValueError: Raised when the specified value is not found in the deque.

Example: Removing an Element by Value

In this example, the first occurrence of a given value is removed from the deque:

from collections import deque
# Create a deque with integer elements
numbers = deque([10, 20, 30, 20, 40])
# Remove the first occurrence of 20
numbers.remove(20)
print(numbers)

The output of the code is:

deque([10, 30, 20, 40])

In the example above, the remove() method removes only the first occurrence of 20 from the deque, leaving the second occurrence of 20 intact.

Codebyte Example

In this example, multiple elements are removed from the deque, one at a time:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours