In this Python post, we’ll learn how to use Python list index method. We’ll be using multiple examples to practically understand how it works.
We’ll be using a step by step explanation with proper practical examples for better understanding.
So let’s not wait anymore and just dive right into the implementation of Python list index method.
- What is Python List Index Method?
- Syntax of Index Method
- Implementing Python List Index Method (Multiple Easy Examples)
- Example 1: Get Index of an Item in List
- Example 2: Index Method with Specific Starting Point
- Examples 3: Index Method with Specific Starting and Ending Point
- Example 4: Value Error if Item not Found
What is Python List Index Method?
The index method is used when we want to find/store the index value of a specific Python list item. For instance, if we have 5 items in the list, that means we have a list of index which would range from 0 to 4, covering all the items of list.
Let’s now understand practically of how to use Python list index method using multiple examples.
Syntax of Index Method
listName.index(value, start, end)
As you can see in the above code that the index method takes the real value of item that is present in the Python list. Just pass it to see the index of that item. Start and end is used to specify the range of index where the search should be executed. We’ll understand their role in the practical examples that are specified in the next section.
Implementing Python List Index Method (Multiple Easy Examples)
To practically explain how index method works, see the below examples.
Example 1: Get Index of an Item in List
For that, we’ve to first declare and initialize a Python list. Then we would apply index method on it. See below code:
listofItems= ['green','purple',4,8,3.5] print(listofItems.index('purple')) // prints 1 which means its in index 1
As you can see here that we wanted to fetch the index of string purple so we have passed it to the index method and it returned the index of that item which is 1. It specifies the second position because the index starts from 0.
If you want to get the index of an integer or double specified above, then pass that item to the index method.
Example 2: Index Method with Specific Starting Point
We will see how to specific the starting point from where the search should be executed. See below code:
listofItems= [2,4,5,8,8,4,3.5] print(listofItems.index(4)) // prints 1 print(listofItems.index(4,2)) // prints 5
In the first example, we didn’t specify the range so it will start from the index 0 and as a result, the specified item matched with the second item of the list so it printed the index 1.
In the second example, we specified a starting point from index 2, so it will start searching from there, result is that the item got its match at index 5.
Examples 3: Index Method with Specific Starting and Ending Point
listofItems= [1,2,3,4,5,6,3,7,8,9,3,10,11,12] print( listofItems.index( 3, 3, 9 ) ) // prints 6
You can see that first the item to be searched is specified, then the starting and ending range is specified. As a result, the index method will only search for that specific item’s index in that range. As you can see, that its present in 6th index so the index is printed successfully.
Example 4: Value Error if Item not Found
The Python list index method throws a value error if the specified item is not found in the list. See below example:
listofItems= [1,2,3,4,5,6,3,7,8,9,3,10,11,12] print( listofItems.index( 12, 3, 9) ) // ValueError: 12 is not in list
As you can see that the item 12 is present in the list but it still gives a value error. The reason is simple, we have specified a range in which this value will be checked. The range ends before the item value 12 so it gives a value error.
Let’s try removing this range and see the output:
listofItems= [1,2,3,4,5,6,3,7,8,9,3,10,11,12] print(listofItems.index(12)) // prints 13
Now it is showing that the 12 item has an index value of 13.
So this is how you can easily use Python list index method by following the steps mentioned above. Hope you like this post.
Feel free to ask questions related to Python list index method, if you have any. I’d be happy to answer all.
To conclude, we have discussed the usage of Python list index method with multiple practical examples for in-depth understanding. I’d be happy to have your feedback on this post. Thank you for reading it.