Python remove()
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.
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 elementsnumbers = deque([10, 20, 30, 20, 40])# Remove the first occurrence of 20numbers.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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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