What is the index() Function?
The method index() returns the lowest index in the list where the element searched for appears.
Let's try it out:
If any element which is not present is searched, it returns a ValueError.
Working with Strings
Let's try it with string elements:
Note: When searching strings, index() is case-sensitive. For example, 'Hello'.index('h') will raise a ValueError.
Finding the First Occurrence
Imagine you have more than one instance of an element, then index() will give you the first position where the element appears.
The position returned is 0, because 3 first appears in the first position or the 0th index in Python.
Here is what's happening internally: The index is going through all values starting from the 1st position (0th index), looking for the element you are searching for. When it finds the value - it returns the position and exits the system. However, this is not too efficient when going through a large list, and you need to get the position of something towards the end of the list.
Using Start and Stop Parameters
So the syntax is: list_name.index(element, start, stop).
Here the start and stop values are optional. In fact, only use it when entirely sure about the range; otherwise, you will get a ValueError, as shown below.
Finding All Occurrences
Since the index() only returns the first match to an object, you can use list comprehension, or generator expression if you need the positions of more matches in the list. Here is how:
list_numbers = [3, 1, 2, 3, 3, 4, 5, 6, 3, 7, 8, 9, 10]
g = (i for i, n in enumerate(list_numbers) if n == 3) # Generator expression
print("Generators store values, the first value here is:", next(g), ". Then the next is:", next(g), "followed by ", next(g),"and finally ", next(g))
# Generators store values, the first value here is: 0. Then the next is: 3 followed by 4 and finally 8Generators are memory-efficient because they yield items one at a time instead of storing all results like list comprehensions.
Final Thoughts
Congrats, you have just learned about the index() function in Python! You have seen how it can help you work with lists. You have been introduced to some new concepts as well. For more on list comprehension, check out our Python List Comprehension tutorial. Our Python Iterator Tutorial has more on generators and generator expressions.

I have worked in various industries and have worn multiple hats: software developer, machine learning researcher, data scientist, product manager. But at the core of it all, I am a programmer who loves to learn and share knowledge!
FAQs
What happens if multiple values match the element I'm searching for?
The index() method only returns the index of the first occurrence of the matching element. If you need all positions, use a list comprehension with enumerate().
Can I use index() with tuples or strings?
Is there a way to handle ValueError gracefully?
How can I search from the end of the list?
How is index() different from find()?
Can I use index() with nested lists?
What is the "list index out of range" error?
It occurs when code tries to access an index outside a list's valid range. A list with 5 elements only has valid indices 0 through 4, so requesting index 5 or higher raises this error. Checking an index against len() before accessing it prevents the error.
What is the "list indices must be integers" error?
It occurs when a non-integer value, such as a float or string, is used as a list index. Lists only accept integer indices, so passing anything else triggers this error. Converting the value with int() before indexing resolves the issue.


In Python, a data structure helps you organize and store data efficiently. One common and versatile structure is a list, which can hold different types of data in a specific order. Python also provides many functions or methods for working with lists.
In this tutorial, you will learn about the Python
index()function. Theindex()method searches an element in the list and returns its position/index. First, this tutorial will introduce you to lists, and then you will see some simple examples of how to work with theindex()function.To easily run all the example code in this tutorial yourself, you can create a DataLab workbook for free that has Python pre-installed and contains all code samples.
What is Indexing in Python?
In Python, indexing refers to the process of accessing a specific element in a sequence, such as a string or list, using its position or index number. Indexing in Python starts at 0, which means that the first element in a sequence has an index of 0, the second element has an index of 1, and so on. For example, if we have a string "Hello", we can access the first letter "H" using its index 0 by using the square bracket notation:
string[0].Python's built-in
index()function is a useful tool for finding the index of a specific element in a sequence. This function takes an argument representing the value to search for and returns the index of the first occurrence of that value in the sequence.If the value is not found in the sequence, the function raises a ValueError. For example, if we have a list
[1, 2, 3, 4, 5], we can find the index of the value3by callinglist.index(3), which will return the value2(since3is the third element in the list, and indexing starts at 0).The
index()function is a powerful tool in Python as it simplifies the process of finding the index of an element in a sequence, eliminating the need for writing loops or conditional statements. This function is especially useful when working with large datasets or complex structures, where manual searching could be time-consuming and error-prone.