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.
Basic Example
num_list = [10,20,30,40,50,60]
num_list[-1]
Output
60
In this tutorial, you’ll learn how to get the last element of a list in Python using different methods.
Using Index
The index operator and the -1
index gets 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
+—+—+—+—+—+—+
Code
num_list = [10,20,30,40,50,60]
num_list[-1]
Output
50
Using Pop method
The pop method removes the last element of a list and returns it.
Using this method will remove the last element and return it. If you need to reuse the list later, you should not use this method.
Code
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,
- 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,
- 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.
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,
- 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,
- Use the number N with the negative index
- It’ll return the last N elements
Code
num_list = [10,20,30,40,50,60]
num_list[-3:]
Output
[40, 50, 50]