Python list index() Method Explained with Practical Examples

by Bharat Arora · Updated on November 7, 2025

In Python, a data structure helps you store and organise data efficiently. One of the
most common and versatile data structures is a list. A list can hold different types of data in a specific order—numbers, strings, or even other lists.

Python also gives you several built-in functions to work with lists efficiently. One of
them is the index() function. It’s simple but powerful. In this Python tutorial, you’ll learn what the index() Function does, how it works, and how you can use it to make your code cleaner and faster.

What is the index() Function in Python?

What is the index() Function in Python?

The index() Function helps you find the position of an element in a list or string. It
searches for the first match and returns its index number (or position).

Here’s the idea:

If you have a list [1, 2, 3, 4, 5] and you want to find where three is located, you
simply write:

numbers = [1, 2, 3, 4, 5]
position = numbers.index(3)
print(position)

This will output 2, because in Python indexing, the count starts at 0. That means the
first element is at index 0, the second at 1, and so on.

What Is Indexing in Python?

What Is Indexing in Python?

Indexing in Python means accessing a specific element in a sequence (like a list or a
string) by its position.

For example:
text = “Hello”
print(text[0])

The output will be “H”. Here, you accessed the first string element index using the
square bracket notation.

In short, Python indexing lets you grab data directly without looping through the entire
structure.

Why Use the index() Function?

Why Use the index() Function?

The Python index() Function saves you from writing manual loops to find positions. It’s
faster, cleaner, and perfect for large datasets or complex structures.

If the value you’re looking for isn’t in the list, a ValueError in Python is raised. For
instance:

my_list = [10, 20, 30]
print(my_list.index(50))

This will cause an error because 50 doesn’t exist in the list.

That’s why it’s always good to check if an element exists before calling index().

Syntax of the index() Function

Here’s how the index() method looks in Python:
list_or_string_name.index(element, start_pos, end_pos)

Parameters Explained:

Beyond laws, ethical issues are equally important. Following ethical web scraping
practices builds trust and credibility. It also ensures fairness toward website owners and users.

Parameters Explained:

  • Element:The item or character whose position you want to find.
  • start_pos parameter
    (optional):
    Defines where to start searching in the list or string.
  • end_pos parameter
    (optional):
    Defines where to stop searching.

The start_pos and end_pos help narrow down your search.

Example:

fruits = [“apple”, “banana”, “apple”, “cherry”]
pos = fruits.index(“apple”, 1)
print(pos)

Output: 2

Here, the Function skips the first “apple” because we started searching from position1.

How the index() Function Works with Strings

The index() function also works perfectly with Python strings.
Since a string is like a list of characters, you can search for characters or even substrings.

Example:

word = “programming”
print(word.index(“g”))

Output: 3

This shows the first position of “g” in the string “programming”.

Handling Errors Gracefully

If the element you’re looking for doesn’t exist, Python raises a
ValueError.
You can avoid this by using a simple check before searching:
colours = [“red”, “green”, “blue”]
if “yellow” in colours:
print(colors.index(“yellow”))
else:
print(“Element not found!”)
This is a clean way to prevent errors during your Python list operations.
By default, index() the lowest index in Python where the element appears.
If you want to find the second lowest index, use the start_pos parameter to begin searching after the
first index:
nums = [5, 3, 7, 3, 9]
First = nums.index(3)
second = nums.index(3, first + 1)
print(second)
Output: 3
This method helps when your list indexing involves duplicate values

List Index in Python

List Index in Python

As mentioned earlier, if you want to find the position of an element in a list in
Python, you can use the index() method. This built-in Function quickly locates the element and returns its index
within the list.

Example 1. Identifying the position of a vowel within a list of vowels.

# List of vowels
vowel_list = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
# Let’s find the index of the letter u
index_of_u = vowel_list.index(‘u’)
# Print the index
print(‘The index or position of u in the list is: ‘, index_of_u)

Output –

code

Example 2. How to Locate the Position of a String in a Python List.

# List of strings
names = [“Mike Ehrmantraut”, “John Doe”, “Jessy Pinkman”, “Heisenberg”, “Walter White”]
# Index of Jessy Pinkman
index = names.index(“Jessy Pinkman”)
# Print the result
print(“The position of Jessy Pinkman is: “, index)

Output –

code

Example 3 – Finding an Element That Isn’t Present.

# List of strings
names = [“Mike Ehrmantraut”, “John Doe”, “Jessy Pinkman”, “Heisenberg”, “Walter White”]
# Index of Jane Doe
index = names.index(“Jane Doe”)
# Print the result
print(“The position of Jane Doe is: “, index)

Output –

code

String Index in Python

In Python, a string is viewed as a sequence of individual characters. This means you can
use the same index() method on strings just like you do with lists. The syntax and functionality remain the same.

Example 1 – Searching for a Name in a String of Names.

# String of names
names = “Joey, Chandler, Phoebe, Monica, Rachel, Ross, Phoebe”
# Index of Phoebe
index = names.index(“Phoebe”)
# Print the result
print(“The index of Phoebe is: “, index)

Output –

code

Explanation –

In the example above, the name “Phoebe” appears twice in the string of names. The
index() method is used to find its position. The output, 14, indicates that “Phoebe” starts at index 14 in the
string. Note that the method returns the index of the first occurrence of “Phoebe” in the string.

Playing With Parameters

In Python, the index() method accepts three parameters. The first parameter is the
element whose index you want to locate. The second and third are the starting and ending positions in the list or
string where the search should begin and end.

Example 1 – Working With the Start Parameter.

Next, you can use the start parameter to define where the search should begin.
# String of names
names = ” Joey, Chandler, Phoebe, Monica, Rachel, Ross, Phoebe”
# Index of Phoebe
index = names.index(“Phoebe”, 15)
# Print the result
print(“The index of Phoebe is: “, index)

Output –

code

Explanation –

This time, the output is 40 for the exact string and element. That’s because the start
parameter is set to 15, meaning the search begins from the 15th index. The first “Phoebe” occurs at index 14, so the
method returns the next occurrence, which is at index 40.

Example 2 – Using Start and End Parameters

In this example, you’ll define both the start and end positions to narrow the search
range within a list.

# List of numbers
numbers = [4, 3, 7, 19, 21, 23, 7]
# Index of 7
index = numbers.index(7, 3, 6)
# Print the result
print(“The index of 7 is: “, index)

Output –

code

Explanation –

You might wonder why a ValueError appears even though seven occurs twice in the list.
That’s because the start and end positions are set to 3 and 6, respectively.

The first seven are at index 2, and the last one is at index 6. Remember — the end_pos
parameter searches up to (end_pos – 1). So, if end_pos = 6, Python searches only up to index 5.

Since seven doesn’t appear between indices 3 and 5, the index() method raises a
ValueError.

Conclusion

In this Python tutorial, you learned how the index function helps find the position of
elements quickly and easily. Through clear Python index examples, you got hands-on experience with lists and
strings.

We started with the basics of the Python index function, covered its syntax, and
explained key Python index parameters like the start_pos parameter and end_pos parameter. You also saw how to handle ValueError in Python and how the same function works for both lists and strings.
Whether you’re finding a list index in Python or a string index in Python, this method simplifies your data
organisation in Python. It’s one of the most useful Python built-in functions for clean and efficient code.

If you want to master concepts like this, join our Python training course at our IT
training institute in Jaipur—where learning meets real-world coding experience and practical application.

Frequently Asked Questions (FAQ)

1. What happens if multiple values match the element I’m searching for?

When using the Python index function, it only returns the first occurrence of the
matching element. If you need all matching positions, you can use a list comprehension with the enumerate()
function.

Example:
my_list = [1, 2, 3, 2, 4]
indexes = [i for i, x in enumerate(my_list) if x == 2]
print(indexes) # Output: [1, 3]

This approach is simple, clean, and works well for real-world Python list
examples.

2. Can I use index() with tuples or strings?

Yes! The index() method works on any Python sequence, including tuples and
strings.

Example with a tuple:
my_tuple = (1, 2, 3, 2)
print(my_tuple.index(2)) # Output: 1

Example with a string:
text = “hello world”
print(text.index(‘o’)) # Output: 4

This shows how versatile Python built-in functions can be across multiple data
types.

3. How do I find an element starting from the end of the list?

The index() method doesn’t directly support reverse searching. To find the last
occurrence, reverse the list manually or use a simple trick:

my_list = [1, 2, 3, 2, 1]
last_index = len(my_list) – 1 – my_list[::-1].index(2)
print(last_index) # Output: 3

This helps you perform an efficient Python reverse search without extra libraries.

4. What does the “list index out of range” error mean?

The list index out of range error appears when you try to access an index that doesn’t exist.

For example, if a list has five elements, valid indices range from 0 to 4. Trying to access index five will trigger an error.

To avoid this, always check index limits using the len() function:
my_list = [10, 20, 30]
if 2 < len(my_list):
print(my_list[2])

This small check prevents unexpected crashes and improves your Python list operations.

Bharat Arora

12+ years as a web developer, Bharat has worked in the biggest IT companies in the world. He loves to share his experience in web development.

Bharat Arora

12+ years as a web developer, Bharat has worked in the biggest IT companies in the world. He loves to share his experience in web development.

Leave a comment

Your email address will not be published. Required fields are marked *

Call Our Training Course Specialist for: