The Python index() method returns the index position at which an item is found in a list or a string. index() returns the lowest resulting index for the item. A ValueError is returned if the specified item does exist in the list.
You may want to find the index of a particular element within your data. For example, say you have a list of the top ten baked goods sold at a bakery. You may want to find out the position of Banana Cake in that list.
That’s where the Python index() method comes in. index() returns the index value at which a particular item appears in a list or a string. For this tutorial, we are going to look at the index() method in-depth, with reference to an example.
Python index() Method
The Python index() method returns the index position of an item in a list or a character or range of characters in a string. This method accepts one argument: the item you want to find in your list or string.
The index() method uses the same syntax whether you’re looking for an item in a list or a string. This is because the items in both the list and string data types are identified by index numbers.
Let’s look at the syntax for the index() method:
The index() string method takes three parameters:
- value is the value for which we want to search in our string or list (required).
- start_pos is the index position at which our search should begin (optional).
- end_pos is the index position at which our search should end (optional).
If the item for which you are looking is not in the final list, the interpreter will return a Python ValueError.
index() returns the first instance of an item in a list or a character or phrase in a string.
If you want to find the second instance of a list item or string character or phrase, you need to find the first instance. Then, you can begin your search at the character after the first instance begins. This means that Python will not find the instance that you have already found again.
Python List index()
The list index() Python method returns the index number at which a particular element appears in a list. index() will return the first index position at which the item appears if there are multiple instances of the item.
Python String index() Example
Say that you are the organizer for the local fun run. You have been asked to create a program that allows people to find out their rank in the race.
You have a list of every participant that is ordered by the times in which they completed the race. Your goal is to create a program that gets the index value of an element in a list.
We could find the index value of a person in our list of participants using this code:
participants = ['Leslie Tucker', 'Peter Free', 'Luke Oliver', 'Ernie Bolton'] participant_name = 'Ernie Bolton' index_of_participant = participants.index(participant_name) print(index_of_participant)
Our code returns the index of the element for which we are looking:
On the first line of our code, we declare a Python array called participants. This array stores the names of every participant in the race, ordered by their finishing times in the race. On the next line, we declare a Python variable that stores the name of the person whose rank we want to retrieve.
Next, we use the built-in Python index() method to find the index of our participant’s name, which in this case is Ernie Bolton. On the final line, we print out the index value that the index() method returned. Because Ernie Bolton appears at index position 3 in our list, our program returns 3.
The index() method only returns the first occurrence of an item in an array. So, if you have multiple of the same value in an array, only the index position for the first value will be returned.
In addition, if the item you’re looking for does not exist in an array, a ValueError will be returned. Here’s an example of our above program searching for Lindsay Paulson in our list, who did not participate:
participants = ['Leslie Tucker', 'Peter Free', 'Luke Oliver', 'Ernie Bolton'] participant_name = 'Lindsay Paulson' index_of_participant = participants.index(participant_name) print(index_of_participant)
Our program returns:
ValueError: 'Lindsay Paulson' is not in list
Python String index()
The Python string index() method finds index value at which a particular substring appears in a string. If the Python substring is found, the index() method will return the index value at which the substring starts. Otherwise, an error is raised.
Python String index() Example
Say that we have a string of student names, and we want to know when the name Joey appears in our string. We could use the following code to accomplish this task:
student_names = 'Judith Joey Fred Luke Linda' index_position_of_joey = student_names.index('Joey') print(index_position_of_joey)
Our code returns: 7.
We first declare a variable called student_names that stores our student names in a string. Then, on the next line, we use the index() function to find the index position at which Joey starts in the string. Finally, we print out that value to the console, which in this case was 7.
Let’s say that there are two “Joey”s in our class. We want to find the second instance of his name in the string. We know that the first Joey’s name starts at the index value 7. We can use the start parameter to skip over all characters before the index value 7 and continue our search.
Let’s find the second “Joey” sequence of characters in our list:
student_names = 'Judith Joey Fred Luke Linda Joey' index_position_of_joey = student_names.index('Joey', 8) print(index_position_of_joey)
Our code returns: 28.
By specifying a start parameter of 8, our code knows not to look for Joey in any index position at 7 or below. So, our code returns 28, which is the index value at which the second Joey appears in our string.
Specifying an End Parameter
Similarly, you can specify an end parameter to indicate when the search should end. Say we’re looking for Fred’s name in our string. We know that his name comes before the second Joey.
To find Fred’s name, knowing the name comes after the second Joey, we could use this code:
student_names = 'Judith Joey Fred Luke Linda Joey' index_position_of_joey = student_names.index('Fred', 8, 28) print(index_position_of_joey)
Our code returns: 12.
The index() method only conducts a search between the index values 8 and 28 in our string. This method returns 12, the position at which Fred’s name starts in our string.
Now, let’s say that we are looking for Hannah in our string, who has not yet been added to our class. This will result in a ValueError being returned by our program because her name does not yet exist. Here’s this scenario written out in code:
student_names = 'Judith Joey Fred Luke Linda Joey' index_position_of_joey = student_names.index('Hannah') print(index_position_of_joey)
Our code returns:
ValueError: substring not found
The Python index() method finds the index position of an item in an array or a character or phrase in a string. The syntax for this method is: string.index(item, start, end).
In this tutorial, we discussed the Python array index() method and the string index() method. We explored the syntax for each of these methods and a few examples to accompany the knowledge.
Do you want to learn more about the Python programming language? Read our How to Learn Python guide. This guide contains a list of top learning resources that can help you acquire the skills you need to become a Python expert.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.