Get Last Index of List in Python (3 Examples)
In this Python tutorial, you’ll learn how to access the last index of a Python list.
The table of content is structured as follows:
Let’s dive into the code!
Create Sample List
The first step will be to create a sample integer list named my_list
to use as an example in this tutorial.
my_list = [1, 2, 3, 4, 5] print(my_list) # [1, 2, 3, 4, 5]
As shown, our list contains five integers. Let’s see how to get the last index on our list!
Example 1: Get Last Element Using Indices
First, it might be interesting to know how to retrieve the last element in my_list
. In our case, we know that the last element is 5, but in cases that we do not know, you can retrieve it as follows.
last_element = my_list[-1] print(last_element) # 5
Now, we can use this last element to retrieve the last index. Let’s see some examples!
Example 2: Get Last Index Using index() Method
We can access the last index in my_list
using the last element called in the index() method.
last_index = my_list.index(5) print(last_index) # 4
The previous Python output shows how my_list.index(5)
returns the index of the integer ‘5’ in my_list
, which is 4.
Example 3: Get Last Index Using for Loop
Another simple way to get the last index in my_list
is using a for loop.
for i in range(len(my_list)): if my_list[i] == 5: last_index = i break print(last_index) # 4
As you can see in the previous Python output, the for loop iterates over each index in the list using the range() function. Then, it checks if the element at that index is equal to 5. If so, it assigns the index to last_index
and breaks out of the loop.
Example 4: Get Last Index Using List Comprehension
In this last example, we will use list comprehension to print the index of the last element in my_list
.
last_index = [i for i in range(len(my_list)) if my_list[i] == 5] print(last_index) # [4]
Here, the list comprehension generates a new list containing the index of 5, which is the 4th index, as we already know.
In our list, this element can only be found once, but if your last element is repeated in your list, you might want to consider other methods to retrieve this last index.
Video, Further Resources & Summary
Do you need more explanations on how to get the last index in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some other tutorials on Statistics Globe:
- Get Next Element in List in Python
- Get First Index of List in Python
- Get Index of Item in 2D List in Python
- Find Index of Same Element in List in Python
- Python Overview
This post has shown how to get the last index in a list in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.