Method signature
index(elem[, start[, stop]])
Parameters
elem - The element to be found in the deque.
start - Position in deque at which the search should start.
stop - Position in deque at which the search should stop.
Overview
- The index() method 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 positions/indexes start and stop.
Example1
import collections
# create two complex number objects
complexNum1 = complex(20,-1)
complexNum2 = complex(21,-1)
# Add complex numbers to the deque
d1 = collections.deque()
d1.append(complexNum1)
d1.append(complexNum2)
# 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))Output
Complex number (21-1j) was found in the deque at index:1Example2
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))Output
The word 'hill' is found at index:6