Picture this, you have a list of 20 students and their marks stored in a list in Python in the order of decreasing marks. Now, you want to find out the details of the student who has secured the 3rd position in the class. How would you go about doing the same?

This is where indexing in Python comes to the picture. There are tons of methods available in Python that make your life a lot easier while working with data structures such as lists, dictionaries, tuples, and so on. One such method is the index() method in Python.

The Python index() method helps you find the index position of an element or an item in a string of characters or a list of items. It spits out the lowest possible index of the specified element in the list. In case the specified item does not exist in the list, a ValueError is returned.

This guide will take you through an in-depth explanation of how index() in Python behaves in different situations. And you will support this claim with different hands-on examples.

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
Learn From The Best Mentors in the Industry!

Index() in Python

As discussed earlier, the index() in Python returns the position of the element in the specified list or the characters in the string. It follows the same syntax whether you use it on a list or a string. The reason being that a string in Python is considered as a list of characters starting from the index 0. 

Let’s look at the syntax of the index() in Python.

>>> list_or_string_name.index(element, start_pos, end_pos)

The parameters are as follows:

  • Element: This is the list element or the string character whose lowest index/position will be returned.
  • Start_pos: This specifies the position of the list item or the character of the string from where the search begins.
  • End_pos: This specifies the position of the list element or the character of the string from where the search begins.

The parameters start_pos and end_pos are optional.

Note: In case the element or the character specified as the element parameter does not exist in the iterable (list or string), then it returns a ValueError.

Also, the index() methods in Python finds and returns the lowest possible index of the element in the list or string. If you want to find the second-lowest index of the same element, you can find the lowest index first, and then use this index as the start_pos parameter to begin your search from there. And then the returned value will be the second-lowest index of the same element.

Prepare Yourself to Answer All Questions!

Automation Testing Masters ProgramExplore Program
Prepare Yourself to Answer All Questions!

List Index in Python

As discussed earlier, if you want to find the position of an element in a list in Python, then you can use the index() method on the list. 

Example 1. Finding the Index of a Vowel in 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 - 

listofvowels

Example 2. Finding the Index of a String in a List of Strings.

# List of strings
names = ["Mike Ehrmantraut", "John Doe", "Jessy Pinkman", "Hiesenberg", "Walter White"]

# Index of Jessy Pinkman
index = names.index("Jessy Pinkman")

# Print the result
print("The position of Jessy Pinkman is: ", index)

Output - 

listofstrings.

Example 3 - Finding an Element That Does Not Exist.

# List of strings
names = ["Mike Ehrmantraut", "John Doe", "Jessy Pinkman", "Hiesenberg", "Walter White"]

# Index of Jane Doe
index = names.index("Jane Doe")

# Print the result
print("The position of Jane Doe is: ", index)

Output - 

Findinganelement

String Index in Python

In Python, a String is considered as a list of characters. Needless to say, you can also apply the same index() method on strings as well. The syntax of the method also remains the same.

Example 1 - Finding 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 - 

stringofnames

Explanation - 

In the example stated above,  in the string of names, the name “Phoebe” appears twice. It has used the index method on the string to find the index of the name “Phoebe”. This returns 14 as the output. This means that the string “Phoebe” starts from the 14th index in the string of names. Also, it has returned the index of the first occurrence of the name “Phoebe” in the string of the names.

Playing With Parameters

For the index() method in Python, you can pass 3 parameters. The first one is the element whose index you want to find. The second and the last ones are the starting and ending indices of the string or list where you want to begin and end the search.

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

Example 1 - Working With the Start Parameter.

Next, use the start parameter to specify the beginning position of our search.

# 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 - 

StartParameter

Explanation - 

This time the output is 40 for the same string and the input element. This is so because you have mentioned the start parameter as 15, which means that the search begins from the 15th index of the string. And the first occurrence of “Phoebe” is 14. That’s why it has returned the next occurrence of the word “Phoebe” which is 40.

Example 2 - Working With the Start and End Parameters

This example will specify both the start and the end positions in 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 - 

EndParameters

Explanation - 

You might wonder, even though 7 occurs twice in the list, it still outputs a ValueError. This is so because you have mentioned the start and end positions as 3 and 6 respectively in the list. The first occurrence of 7 is at index 2 and the last is at index 6. Please note that when you specify an ending index, it actually searches up to the index (end_pos - 1). In this case, it searches up to the 5th index if you specify the end_pos to be 6. And between indices 3 and 5, the element 7 does not occur even once, hence it outputs the ValueError.

Unleash a High-paying Automation Testing Job!

Automation Testing Masters ProgramExplore Program
Unleash a High-paying Automation Testing Job!

Conclusion

In this article, you explored the index in Python and went through some practical examples that could help you gain hands-on experience on the topic. It started with a basic discussion of the index method, and then discussed the syntax of the method, along with the possible parameters. 

It also saw how the same method can be applied on both lists and strings through practical examples and multiple scenarios. In the end, you explored how one can use the other two parameters to limit the search.  

Have any questions for us? Mention them as comments below, and our experts will get back to you as soon as possible!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 15 Apr, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 2 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 3 Apr, 2024

11 Months$ 1,499
Full Stack Developer - MERN Stack

Cohort Starts: 3 Apr, 2024

6 Months$ 1,449