Skip to content

Commit cef38b9

Browse files
Add remove() method documentation for deque (#7780)
* Add remove() method documentation for deque * minor fixes * Update remove.md * Update remove.md ---------
1 parent b63f52d commit cef38b9

File tree

1 file changed

+81
-0
lines changed
  • content/python/concepts/deque/terms/remove

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
Title: 'remove()'
3+
Description: 'Removes the first occurrence of a specified value from a deque.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Structures'
7+
Tags:
8+
- 'Data Structures'
9+
- 'Deque'
10+
- 'Methods'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`remove()`** method removes the first occurrence of a specified value from a [deque](https://www.codecademy.com/resources/docs/python/collections-module/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.
17+
18+
## Syntax
19+
20+
```pseudo
21+
deque.remove(value)
22+
```
23+
24+
**Parameters:**
25+
26+
- `value`: The item to be removed from the deque. Only the first occurrence is removed.
27+
28+
**Return value:**
29+
30+
The `remove()` method returns `None`.
31+
32+
**Exceptions:**
33+
34+
- `ValueError`: Raised when the specified value is not found in the deque.
35+
36+
## Example: Removing an Element by Value
37+
38+
In this example, the first occurrence of a given value is removed from the `deque`:
39+
40+
```py
41+
from collections import deque
42+
43+
# Create a deque with integer elements
44+
numbers = deque([10, 20, 30, 20, 40])
45+
46+
# Remove the first occurrence of 20
47+
numbers.remove(20)
48+
49+
print(numbers)
50+
```
51+
52+
The output of the code is:
53+
54+
```shell
55+
deque([10, 30, 20, 40])
56+
```
57+
58+
In the example above, the `remove()` method removes only the first occurrence of `20` from the deque, leaving the second occurrence of `20` intact.
59+
60+
## Codebyte Example
61+
62+
In this example, multiple elements are removed from the deque, one at a time:
63+
64+
```codebyte/python
65+
from collections import deque
66+
67+
# Create a deque with string elements
68+
colors = deque(['red', 'blue', 'green', 'blue', 'yellow'])
69+
70+
print('Original deque:', colors)
71+
72+
# Remove the first occurrence of 'blue'
73+
colors.remove('blue')
74+
75+
print('After remove("blue"):', colors)
76+
77+
# Remove another element
78+
colors.remove('red')
79+
80+
print('After remove("red"):', colors)
81+
```

0 commit comments

Comments
 (0)