Python list elements can be accessed using its index.
You can get the last element of a list in python using the list_name[-1] statement.
In this tutorial, you’ll learn how to get the last element of a list in Python using different methods.
If You’re in Hurry…
You can get the last element of a list in python using the index -1, as demonstrated below.
num_list = [10,20,30,40,50,60]
num_list[-1]
Output
60
If You Want to Understand Details, Read on…
There are different methods available in core python, and there are also other libraries to get the list items. Let us learn those in detail.
Table of Contents
Using Index
You can use the index operator and the -1
index to get the last element of a list in python.
The index
gives access to the sequence’s element. Here the sequence is a list object.
As represented below, each object in the list element has a positive
index and a negative
index.
+—+—+—+—+—+—+
| 0 | 1 | 2 | 3 | 4 | 5 | 6<= positive indexes
+—+—+—+—+—+—+
| 10 | 20 | 30 | 40 | 50 | 60 | <= elements
+—+—+—+—+—+—+
| -6| -5 | -4 | -3 | -2 | -1 | <= negative indexes
+—+—+—+—+—+—+
You can use the -1
index to get the last element.
Code
num_list = [10,20,30,40,50,60]
num_list[-1]
Output
50
Using Pop method
You can use the pop method to get the last element of a list in Python.
Using this method will remove the last element and return it. If you need to resue the list later, you should not use this method.
Code
The code below demonstrates how to get the last element of a list in Python.
num_list = [10,20,30,40,50,60]
num_list.pop(-1)
Output
50
Using More-itertools
The more-itertools library provides additional building blocks for the functionalities available in the itertools library. It offers additional recipes for working with the Python Iterables.
You can use the last() function to get the last element from the list.
It accepts two parameters.
iterable
– A list to get the last element from.default
–default
value needs to be returned when the list is empty. If thedefault
value is not passed, it’ll raise aValueError
exception. It is an Optional parameter.
Installation
You can install the more-itertools
package using the below statement.
pip install more-itertools
To install the package directly from the Jupyter notebook, you can prefix the %
symbol.
Code
The code below demonstrates how to use the last()
function to get the last element of a list.
import more_itertools as mitr_tools
num_list = [10,20,30,40,50,60]
mitr_tools.last(num_list)
Output
50
Prevent IndexError
When you use the -1
index to get the last element of a list, it returns an IndexError
exception when the list is empty.
Code
num_list = []
num_list[-1]
Output
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/var/folders/gb/19ly8vfs7t53plwx43wx1q3m0000gn/T/ipykernel_44817/4055114848.py in <module>
1 num_list = []
2
----> 3 num_list[-1]
IndexError: list index out of range
To avoid the ValueError
while using the indexing method and the list is empty, you can use the slice notation to get the last element.
It returns an empty list when there are no elements in the list.
Code
num_list = []
num_list[-1:]
Output
[]
Using And Operator to Avoid Value error
Using the and
keyword, you can check if the list is empty before trying to get the last element of a list.
In this case, the ValueError
will not be thrown.
Code
The code below demonstrates how the and
keyword works when the list is empty. It provides an empty list.
num_list = []
value = num_list and num_list[-1]
value
Output
[]
Code
The code below demonstrates how the and
keyword works when the list is NOT empty. It provides an empty list.
num_list = [10,20,30,40,50,60]
value = num_list and num_list[-1]
value
Output
The last element of the list is returned.
50
Get Second Last Element of List
To get the second last element of a list, you can use the -2
index.
num_list = [10,20,30,40,50,60]
num_list[-2]
Output
50
Get the Last N elements of the List
To get the last N elements of the list, you can use the number N with the negative index. It’ll return the last N elements.
Code
The code below demonstrates how to get the last three elements of the list.
num_list = [10,20,30,40,50,60]
num_list[-3:]
Output
[40, 50, 50]
Conclusion
You’ve learned how to get the last element of a list in Python.
Additionally, you’ve learned how to avoid the errors when the list is empty while getting the last element of a list in Python. And how to get the last N elements of a list in Python.