How to get last n elements of a list in Python
In this tutorial, we are going to learn about how to get the last n elements of a list in Python.
Consider, we have the following list:
numList = [12, 13, 14, 15, 16]
To access the last n elements from a list, we can use the slicing syntax [ ]
by passing a -n:
as an argument to it .
Here is an example, that gets the last 2 elements of a list:
numList = [12, 13, 14, 15, 16]
lastTwo = numList[-2:]
print(lastTwo) # [15, 16]