Index() Method Of Deque Class In Python

Method Name:



Method Signature:

index(elem[, start[, stop]])

Method Overview:

  • index() searches for an element as defined by the parameter elem and returns the index at which the element is found in the python deque.


  • If the specified element is not in the deque python raises an exception of type ValueError.


  • If the parameters start and stop are provided then searching for the element is restricted between the indexes start and stop.


import collections


# create two complex number objects

complexNum1 = complex(20,-1)

complexNum2 = complex(21,-1)


# Add complex numbers to the deque

d1 = collections.deque()




# Create another complex number which will be used for searching

searchNumber = complex(21,-1)


# Search the deque for the given complex number

pos = d1.index(searchNumber)


# Print results

print("Complex number %s was found in the deque at index:%d"%(searchNumber, pos))




Complex number (21-1j) was found in the deque at index:1



import collections


words = collections.deque(("Jack", "and", "Jill", "went", "up", "the", "hill", "To",

                          "fetch", "a", "pail", "of", "water"))

startIndex  = 2

stopIndex   = 7

what2Find   = "hill"

pos = words.index(what2Find, startIndex, stopIndex)

print("The word '%s' is found at index:%d"%(what2Find, pos))


The word 'hill' is found at index:6


Copyright 2023 ©