index() method returns the index value for an item in a list or a string. In both cases index() returns the lowest resulting index for the item, or
ValueError if the item doesn’t exist in the list.
When you’re working with a string or a list in Python, you may want to find the index of a particular element within your data. For example, you may want to find out the position of
Banana Cake in a list of the top 10 baked goods sold in your bakery. Or you may want to find out where the name
Andrew appears in a string with runner names and the places they ranked in a local race.
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, and discuss how you can use it in your Python code. We’ll go through examples of
index() being used both on a list and on a string. By the end of this article, you’ll be an expert in using the
Python Index for Lists
In Python, each item in a list is assigned an index number, starting with
, that can be used to retrieve a specific item in the list. The list
index() method can be used to search a list for a particular element and return the index value of that element, if it is found.
index() will return the lowest index in the list in which the item appears.
The built-in function
index() takes in one parameter: the item whose index value you want to retrieve. Here’s the syntax for the Python list
Let’s use an example to illustrate how the
index() method can be used on a list.
Say that you are the organizer for the local fun run, and 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, and you want to create a program that gets the index value of an element in a list.
The following program could be used to accomplish this goal:
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:
There’s a lot going on in our code, so we will break it down. On the first line, we declare a list called
participants which stores the names of every participant in the race, ordered by their finishing times in the race. On the next line, we declare a variable that holds the name of the person whose rank we want to retrieve.
Next, we use the
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.
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 Index for Strings
In addition, the string
index() method can be used to find the index value at which a particular substring appears in a string. If the substring is found, the
index() method will return the index value at which the substring starts; if the substring is not found, an error will be raised.
The syntax for the string
index() method is as follows:
string_name.index(value, start_pos, end_pos)
index() method takes three parameters:
- value is the value for which we want to search in our string (required)
start_posis the index position at which our search should begin (optional)
end_posis the index position at which our search should end (optional)
Let’s use an example to showcase the string
index() method in action. 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:
Let’s break down the above example. On the first line, we 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.
Now, let’s say that there are two Joeys in our class, and 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. Here’s the code we could use:
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:
As you can see, 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.
Similarly, you can specify an end parameter to indicate when the search should end. So, if we’re looking for Fred’s name in our string, and we know that his name comes before the second Joey, we could use the following code to conduct a search:
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:
index() method only conducts a search between the index values 8 and 28 in our string, and returns 12, which is 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
index() method can be used to find the index value at which a particular item appears in an array, or the index value at which a substring begins in a larger string. The
index() method can be useful in a number of scenarios. For example, if you had an array that stores your inventory and wanted to check if the iPhone 11 XR was in stock, you could use the
In this tutorial, we discussed the Python array
index() method and the string
index() method. We explored the syntax for each of these methods, what happens when a value is not found, and we used a few examples to showcase how these methods worked in a real program.
Now you have the knowledge you need to use the Python
index() method on strings and lists like a pro!