Table of Contents
In this tutorial, we will learn about the Python List index() method with the help of examples.
The index() method returns the index of the specified element in the list.
Example
animals = ['cat', 'dog', 'rabbit', 'horse']
# get the index of 'dog'
index = animals.index('dog')
print(index)
# Output: 1
1. Syntax of List index()
The syntax of the list index() method is:
list.index(element, start, end)
2. list index() parameters
The list index() method can take a maximum of three arguments:
- element – the element to be searched
- start (optional) – start searching from this index
- end (optional) – search the element up to this index
3. Return Value from List index()
- The
index()method returns the index of the given element in the list. - If the element is not found, a
ValueErrorexception is raised.
Note: The index() method only returns the first occurrence of the matching element.
4. Example 1: Find the index of the 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)
Output
The index of e: 1 The index of i: 2
5. Example 2: Index of the Element not Present in the List
# vowels list
vowels = ['a', 'e', 'i', 'o', 'u']
# index of 'p' is vowels
index = vowels.index('p')
print('The index of p:', index)
Output
ValueError: 'p' is not in list
6. Example 3: Working of index() With Start and End Parameters
# alphabets list
alphabets = ['a', 'e', 'i', 'o', 'g', 'l', 'i', 'u']
# index of 'i' in alphabets
index = alphabets.index('e') # 1
print('The index of e:', index)
# 'i' after the 4th index is searched
index = alphabets.index('i', 4) # 6
print('The index of i:', index)
# 'i' between 3rd and 5th index is searched
index = alphabets.index('i', 3, 5) # Error!
print('The index of i:', index)
Output
The index of e: 1 The index of i: 6 Traceback (most recent call last): File "*lt;string>", line 13, in ValueError: 'i' is not in list
Related posts:
Python Input, Output and Import
Python print()
Python String lstrip()
Python max()
Removing all duplicates from a List in Java
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python iter()
Python Program to Create Pyramid Patterns
Python Program to Count the Number of Occurrence of a Character in String
Python staticmethod()
Python Program to Check the File Size
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python String isdigit()
Remove the First Element from a List
Python Program to Concatenate Two Lists
Python time Module
Python ascii()
Python Program to Randomly Select an Element From the List
Python enumerate()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String startswith()
Java List UnsupportedOperationException
Python Program to Find the Square Root
Python Program to Illustrate Different Set Operations
Python String ljust()
Python Program to Display Fibonacci Sequence Using Recursion
Using a List of Values in a JdbcTemplate IN Clause
Python Program to Print Colored Text to the Terminal
Python divmod()
How to Convert List to Map in Java
Python String expandtabs()