Skip to content

Commit 1628ed8

Browse files
authored
[Term Entry] Python deque: index() (#7755) (#7784)
* [Term Entry] Python deque: index() (#7755) * Adding index.md --------- * minor content fixes * fixed typo * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Lint ---------
1 parent 880fe25 commit 1628ed8

File tree

1 file changed

+96
-0
lines changed
  • content/python/concepts/deque/terms/index

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
Title: 'index'
3+
Description: 'Returns the position of the first occurrence of a specified element within the deque.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Algorithms'
9+
- 'Collections'
10+
- 'Data Structures'
11+
- 'Deques'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`index()`** method in a Python [deque](https://www.codecademy.com/resources/docs/python/collections-module/deque) returns the position of the first occurrence of a specified element within the deque.
18+
19+
## Syntax
20+
21+
```pseudo
22+
deque.index(element, start=0, stop=len(deque))
23+
```
24+
25+
**Parameters:**
26+
27+
- `element`: The element to be found within the deque.
28+
- `start` (Optional): The zero-based inclusive index to specify the start position of the search.
29+
- `stop` (Optional): The index to end the search (default is the length of the deque).
30+
31+
**Return value:**
32+
33+
Returns the index (`int`) of the first matching element; raises a `ValueError` if the element is not found.
34+
35+
> **Note:** This method raises a `ValueError` if the element is not in the deque, which can stop the code if not handled inside a [try and except](https://www.codecademy.com/resources/docs/python/keywords/try) block.
36+
37+
## Example 1: Finding elements by position
38+
39+
In this example, `index()` is used to find the position of elements in different scenarios:
40+
41+
```py
42+
from collections import deque
43+
44+
# Create a deque
45+
dq = deque([1, 2, 3, 1, 4, 5, 1])
46+
47+
# Find the very first position of (4)
48+
print("Very first position of (4):", dq.index(4))
49+
50+
# Find the position of (1) after index=0
51+
start = 0 + 1 # Since the start parameter is inclusive, add 1 to skip index=0.
52+
print("Position of (1) after index=0:", dq.index(1, 1))
53+
54+
# Find the position of (1) in a given window
55+
start = 1
56+
stop = 6
57+
print("Position of (1) between index=1 inclusive and index=6 exclusive:", dq.index(1, 1, 6))
58+
59+
# Find the position of (10), which is not in the deque
60+
try:
61+
print("Position of (10):", dq.index(10))
62+
except ValueError as ve:
63+
print("Trying to find the index of a nonexistent element will cause an error.")
64+
print(ve)
65+
```
66+
67+
The output of this code is:
68+
69+
```shell
70+
Very first position of (4): 4
71+
Position of (1) after index=0: 3
72+
Position of (1) between index=1 inclusive and index=6 exclusive: 3
73+
Trying to find the index of a nonexistent element will cause an error.
74+
10 is not in deque
75+
```
76+
77+
## Codebyte Example: Finding elements by value range
78+
79+
In this example, `index()` finds the position of the element `"apple"` in different scenarios, showing its flexibility and use of parameters:
80+
81+
```codebyte/python
82+
from collections import deque
83+
84+
# Create a deque with some initial elements
85+
fruits = deque(["apple", "banana", "apple", "lemon", "apple", "tomato"])
86+
87+
# Find the index for the first occurrence of "apple"
88+
first_occurrence_of_apple_index = fruits.index("apple")
89+
print("First found index for apple:", first_occurrence_of_apple_index)
90+
91+
# Find the next occurrence of "apple" after the first occurrence
92+
print("Next occurrence:", fruits.index("apple", first_occurrence_of_apple_index + 1))
93+
94+
# Find the index for "apple" between index=1 inclusive and index=4 exclusive
95+
print("Index for apple between (1) inclusive and (4) exclusive:", fruits.index("apple", 1, 4))
96+
```

0 commit comments

Comments
 (0)