• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Last N Elements of List in Python

Get Last N Elements of List in Python

February 10, 2022 Leave a Comment

To get the last n elements of a list using Python, the easiest way is to use slicing.

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]

last_5_numbers = list_of_numbers[-5:]

print(last_5_numbers)

#Output:
[0, 3, 0, -1, 0]

You can also use the islice() function from the Python itertools module in combination with the reversed() function.

from itertools import islice

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]

last_5_numbers= list(islice(reversed(list_of_numbers), 0, 5))
last_5_numbers.reverse()

print(last_5_numbers)

#Output:
[0, 3, 0, -1, 0]


When working with lists of strings, it can be valuable to be able to easily filter and get only specific values from your list.

One such situation where you may want to get only the last few elements of a list.

In Python, we can easily get the last n elements of a list using slicing.

Slicing allows us to get the last n elements from a list in 1 line.

Below shows how we can use slicing in Python to get the last n elements of a list.

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]

last_5_numbers = list_of_numbers[-5:]

print(last_5_numbers)

#Output:
[0, 3, 0, -1, 0]

Using the islice() Function in Python to Get Last N Elements

We can also use the Python islice() function from the Python itertools module to find the last n elements in a list.

First, we need to reverse the list with the Python reversed() function. Then, we use islice, passing 0 and the number of elements we want. Finally, we convert the result back to a list.

Below is an example of how to use the Python islice() function to obtain the last n elements of a list.

from itertools import islice

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]

last_5_numbers= list(islice(reversed(list_of_numbers), 0, 5))
last_5_numbers.reverse()

print(last_5_numbers)

#Output:
[0, 3, 0, -1, 0]

I personally think this method isn’t super useful, as the first method with slicing is so easy, but you might find it interesting or valuable.

Hopefully this article has been useful for you to understand how to get the last n elements of a list in Python.

Other Articles You'll Also Like:

  • 1.  pandas dropna – Drop Rows or Columns with NaN in DataFrame
  • 2.  How to Find the Longest String in List in Python
  • 3.  Python Check if Object is Iterable with hasattr() Function
  • 4.  Remove Every Nth Element from List in Python
  • 5.  Swap Two Values of Variables in Python
  • 6.  Python Replace Space with Underscore Using String replace() Function
  • 7.  Using Python to Create List of Prime Numbers
  • 8.  Python Logging Timestamp – Print Current Time to Console
  • 9.  Drop Last n Rows of pandas DataFrame
  • 10.  Remove Specific Word from String in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x