DEV Community

Cover image for How to Return Index of item in list on Python?
joshua-brown0010
joshua-brown0010

Posted on

How to Return Index of item in list on Python?

The index() technique returns the index of the required element within the list.

The syntax of the list index() technique is:

list.index(element, start, end)
list index() parameters

The list index() technique can take a most of 3 arguments:

 element - the detail to be searched    
start (optional) - begin looking from this index
 end (optional) - seek the detail as much as this index
Enter fullscreen mode Exit fullscreen mode

Return Value from List index()

 The index() technique returns the index of the given element within the list.   
 If the element isn't found, a ValueError exception is raised.
Enter fullscreen mode Exit fullscreen mode

Note: The index() technique most effectively returns the primary prevalence of the matching element

# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']

# index of 'e' in vowels
index = vowels.index('e')
print('The index of e:', index)

# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index('i')

print('The index of i:', index)
Enter fullscreen mode Exit fullscreen mode

Output

The index of e: 1
The index of i: 2

Read more

Top comments (0)