by Sai gowtham

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]

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course