In this article, we will discuss about the usage details of index()
method of Python List.
Introduction
In Python, the list class provides a function index(item, start, end)
, to find an item in the list or a subset of list. Let’s see some more details about it.
Syntax of List index()
The syntax of index()
method of list is as follows,
list.index(item, start, end)
Parameters of List index()
It accepts three Parameters. Out of which, two are optional.
item
: The item that needs to be searched in the list.start
(optional): The index position, from which searching in list will start.end
(optional): The index position, up to which searching in list will be done.
The default value of start
is 0, and the default value of end
is the last item of list. It means if start
, and end
parameters are not provided, then index()
function will look for the given item in whole list.
Return Value of List index()
It returns the index of given item in the list. As indices in list starts from 0, therefore if given item is found at ith
position in list, then it will return its index i-1
.
Important Point:
If the given item does not exist in the list, then it raises the ValueError exception. So, we need to be ready to handle this exception while using the index()
method of List.
Examples of List index()
Let’s see some example of index() method of List in Python.
Example 1: Get index of an item in List
Suppose we have a list of strings, and we want to find the index position of a string ‘Ritika’ in this list. For that, we will pass the string in the index()
function of List. If this string exists in the list, then the index()
method will return its index position. Let’s see the example,
Latest Python - Video Tutorial
listOfNames = ['John', 'Mark', 'Ritika', 'Atharv', 'Smriti'] # Find the index of string 'Ritika' in list idxPos = listOfNames.index('Ritika') print('Index of given item is : ', idxPos)
Output:
Index of given item is : 2
As the list contains the string ‘Ritika’, therefore index() function returned its index position 2.
Example 2: List index() and ValueError exception
What if we try to find the index of an element, that does not exists in the List? In that case, index()
function will raise ValueError exception. Let’s see an example,
listOfNames = ['John', 'Mark', 'Ritika', 'Atharv', 'Smriti'] # Find the index of string 'Shaun' in list idxPos = listOfNames.index('Shaun') print('Index of given item is : ', idxPos)
Output:
Traceback (most recent call last): File "temp.py", line 4, in <module> idxPos = listOfNames.index('Shaun') ValueError: 'Shaun' is not in list
As the string ‘Shaun’ does not exist in the list, therefore the index() method raised the exception ValueError
. If this exception remained unhandled, then it can cause the application to crash. Therefore, we should always be ready to handle this kind of exception.
To avoid this kind of issue, we have two options,
Avoid ValueError exception with List index() method
Always check if list contains the element or not, before looking for its index using index()
method of list. For example,
listOfNames = ['John', 'Mark', 'Ritika', 'Atharv', 'Smriti'] item = 'Shaun' # check if item exists in list if item in listOfNames: # Find the index of string 'Shaun' in list idxPos = listOfNames.index(item) print('Index of given item is : ', idxPos) else: print('Item does not exist in list')
Output:
Item does not exist in list
Handle ValueError exception with List index() method
Handle the exception using try / except
by calling the index()
method of List inside try
block
listOfNames = ['John', 'Mark', 'Ritika', 'Atharv', 'Smriti'] item = 'Shaun' try: # Find the index of string 'Shaun' in list idxPos = listOfNames.index(item) print('Index of given item is : ', idxPos) except: print('Item does not exist in list')
Output:
Item does not exist in list
These are two safe ways to get the index of a value in List
, and avoid ValueError
exception.
Using List index() with optional range parameters
The index() function accepts two optional arguments i.e.
start
(optional): The index position, from which searching in list will start.end
(optional): The index position, up to which searching in list will be done.
We can pass these parameters in index()
function to find the index of an item in a subset of list only i.e. from index start
up to index end
. Let’s see an example,
Suppose we have a a list of names, and we want to find the index of a string in it , but we want to look into a subset of list i.e. from index 2 upto index 4 i.e.
listOfNames = ['Ritika', 'Mark', 'Ritika', 'Atharv', 'Smriti', 'John'] item = 'Ritika' # Find the index of string 'Ritika' in subset of list idxPos = listOfNames.index(item, 1, 4) print('Index of given item is : ', idxPos)
Output:
Index of given item is : 2
Although, list contains the string ‘Ritika’ at index 0 in list, but the index()
function skiiped it, because it looked into a subset of list
only. In this case it was from index 1 till 4, therefore it returned the index of string ‘Ritika’ in that range only.
Summary
This is all that you need to know about the index()
method List in Python. Thanks.
Latest Video Tutorials