Python index

Anonymous contributor's avatar
Anonymous contributor
Published Oct 30, 2025
Contribute to Docs

The index() method in a Python deque returns the position of the first occurrence of a specified element within the deque.

  • 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.index(element, start=0, stop=len(deque))

Parameters:

  • element: The element to be found within the deque.
  • start (Optional): The zero-based inclusive index to specify the start position of the search.
  • stop (Optional): The index to end the search (default is the length of the deque).

Return value:

Returns the index (int) of the first matching element; raises a ValueError if the element is not found.

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 block.

Example 1: Finding elements by position

In this example, index() is used to find the position of elements in different scenarios:

from collections import deque
# Create a deque
dq = deque([1, 2, 3, 1, 4, 5, 1])
# Find the very first position of (4)
print("Very first position of (4):", dq.index(4))
# Find the position of (1) after index=0
start = 0 + 1 # Since the start parameter is inclusive, add 1 to skip index=0.
print("Position of (1) after index=0:", dq.index(1, 1))
# Find the position of (1) in a given window
start = 1
stop = 6
print("Position of (1) between index=1 inclusive and index=6 exclusive:", dq.index(1, 1, 6))
# Find the position of (10), which is not in the deque
try:
print("Position of (10):", dq.index(10))
except ValueError as ve:
print("Trying to find the index of a nonexistent element will cause an error.")
print(ve)

The output of this code is:

Very first position of (4): 4
Position of (1) after index=0: 3
Position of (1) between index=1 inclusive and index=6 exclusive: 3
Trying to find the index of a nonexistent element will cause an error.
10 is not in deque

Codebyte Example: Finding elements by value range

In this example, index() finds the position of the element "apple" in different scenarios, showing its flexibility and use of parameters:

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