Find index of element in list PythonIn this short tutorial, learn to find the index of an element in a list in Python. We look at the code to achieve this with its pros and cons.
Before we delve into how you could find the index of an element in a list, we do a small recap on what lists are, in Python. However, in case you are already familiar with it, you can head straight to the Solution.
Table of Contents - Find index of element in list Python
- Lists in Python - Recap
- Find index of element in list Python
- Code and Explanation
- Limitations and Caveats
- Other Related Concepts
List in Python - RecapLists in Python are an ordered collection of items and these items could be of various data types. Lists are enclosed in square brackets ([ ]) and the items in a list are separated by commas.
Given lists are an ordered collection of items, each element has its unique position, and each element can be accessed by telling Python the position. These positions are called indexes.
Index in a list starts with 0. This essentially means that the first element in the list has an index of 0 and not 1 and subsequently the second element has an index of 1. This concept holds true for most programming languages. If you have trouble understanding this concept at first, just keep in mind that the indexes are all off-by-one.
With that out of the way let us look at how to find the index of an element in list Python.
Find index of element in list in Python:Since the index of lists is used to access elements in Python, it is only natural that one would want to find the index of an element. This may seem easy when dealing with a small list; however, this could become tedious when the length of the list increases.
To facilitate this, Python has an inbuilt function called index(). This function takes in the element as an argument and returns the index. By using this function we are able to find the index of an element in a list in Python.
Syntax of Index()::
list.index(element, start, end)
Here, “list” refers to the name of the list you are looking to search.
- Element - Required, the element whose index you would like to find.
- Start - Optional, the index to start the search.
- End - Optional, the index to end the search.
- Return the index of the passed element
- ValueError is returned if the element is not found
Code and Explanation:
data_types = ["Str", "Int", "Float"] # Searching for “int” print(data_types.index("Int")) #Output - 1
As you can see, we have used the above code to find the index of an element in a list. Next, let us look at an example using the other two parameters.
vowels = ['a', 'e', 'i', 'o', 'i', 'u'] # Searching for “o” within index 1-5 print(vowels.index("o",1,5)) #Output - 3 # Searching for “o” within index 1-3 print(vowels.index("o",0,2)) #Output - ValueError: 'o' is not in list
In the above code, we first search for the element “o” between the 1st and 5th index, and the index is returned as the element is present.
However, the second code snippet searches for “o” between the 0th and 2nd index, and since “o” is at the 3rd index a ValueError is returned as “o” couldn’t be found.
This is how you used the index() function to find the index of an element in a list.
Find index of element in list Python - Limitations and Caveats:
- ValueErrors are returned if the element cannot be found, I would recommend using a try-catch in case you are using it in a bigger piece of code.
- The parameters are case-sensitive and index() would return a ValueError even if the element is present but in a different case. I would recommend using the .upper or .lower methods in case you aren’t sure about the case.
- Index() method only returns the index of the first occurrence, in case you are looking for the subsiding occurrence using the start and stop parameter accordingly.
P.S. If you’d like to learn more about lists, check out these tutorials on checking if a list is empty and removing items from lists.