While working on a Python automation project for a client in New York, I needed to fetch the last few records from a large dataset. The data was stored in a Python list, and I only wanted the last N elements, say, the last 5 transactions.
At first, it seemed simple, but as I explored different use cases, I realized there are multiple ways to achieve this in Python. Some are more efficient, while others are more readable or flexible depending on the situation.
In this tutorial, I’ll show you seven easy methods to get the last N elements of a list in Python. Each method comes from my own experience working with real-world Python projects.
Method 1 – Use Python List Slicing
One of the most common and Pythonic ways to get the last N elements of a list is by using list slicing.
Python slicing is simple, efficient, and works beautifully for most use cases.
# Let's say we have a list of daily sales in USD
sales = [1200, 1350, 1600, 1450, 1750, 1900, 2100, 2300]
# Number of elements we want from the end
n = 3
# Get the last n elements
last_sales = sales[-n:]
print("Last", n, "sales values:", last_sales)You can refer to the screenshot below to see the output.

In this example, sales[-n:] returns the last three elements of the list. This method is fast and works even if the list is large; Python handles it efficiently.
Method 2 – Use Negative Indexing in a Loop
Sometimes, you might want to manually access the last few elements one by one. In such cases, negative indexing can be a handy technique.
sales = [1200, 1350, 1600, 1450, 1750, 1900, 2100, 2300]
n = 3
last_sales = []
for i in range(-n, 0):
last_sales.append(sales[i])
print("Last", n, "sales values:", last_sales)You can refer to the screenshot below to see the output.

Here, the loop iterates from -n to 0, effectively picking the last N elements. While not as concise as slicing, this method helps when you need more control over how elements are processed.
Method 3 – Use Python’s itertools.islice()
When working with large lists or iterables, using the itertools module can be more memory-efficient. The islice() function allows you to extract specific parts of an iterable without converting it fully into a list.
from itertools import islice
sales = [1200, 1350, 1600, 1450, 1750, 1900, 2100, 2300]
n = 4
# Use islice with reversed list
last_sales = list(islice(reversed(sales), n))
last_sales.reverse()
print("Last", n, "sales values:", last_sales)You can refer to the screenshot below to see the output.

This approach is especially useful when you’re working with data streams or large files. It avoids loading unnecessary data into memory, making it great for performance-critical Python applications.
Method 4 – Use Python’s collections.deque()
If you’re dealing with a continuous stream of data, such as real-time stock prices, the deque object from the collections module is perfect.
It allows you to efficiently keep only the last N elements in memory.
from collections import deque
sales = [1200, 1350, 1600, 1450, 1750, 1900, 2100, 2300]
n = 5
# Keep only the last n elements
last_sales = deque(sales, maxlen=n)
print("Last", n, "sales values:", list(last_sales))You can refer to the screenshot below to see the output.

The deque automatically discards older elements as new ones are added. This is a great choice for real-time applications like monitoring or logging systems in Python.
Method 5 – Use a Custom Python Function
Sometimes, it’s helpful to wrap your logic in a function so you can reuse it across multiple projects. Here’s how you can write a reusable Python function to get the last N elements.
def get_last_n_elements(data_list, n):
"""Return the last n elements of a list."""
if not isinstance(data_list, list):
raise TypeError("Input must be a list.")
if n <= 0:
return []
return data_list[-n:]
# Usage
sales = [1200, 1350, 1600, 1450, 1750, 1900, 2100, 2300]
print(get_last_n_elements(sales, 4))This function adds error handling and flexibility. It’s a clean, production-ready Python solution that I often use in my automation scripts.
Method 6 – Use Python’s list() and reversed()
If you ever need the last N elements but in reverse order, you can combine reversed() with slicing.
This is particularly useful when analyzing recent data first.
sales = [1200, 1350, 1600, 1450, 1750, 1900, 2100, 2300]
n = 4
last_sales = list(reversed(sales))[:n]
print("Last", n, "sales values (reversed):", last_sales)This gives you the last N elements starting from the most recent. I often use this approach when analyzing time-series data in Python.
Method 7 – Use List Comprehension
List comprehensions are one of my favorite Python features. They make your code concise and expressive.
sales = [1200, 1350, 1600, 1450, 1750, 1900, 2100, 2300]
n = 3
last_sales = [sales[i] for i in range(len(sales)-n, len(sales))]
print("Last", n, "sales values:", last_sales)While this method is not as efficient as slicing, it’s a great way to understand how Python indexing works. It’s also useful when you want to add conditions or transformations inside the comprehension.
Bonus Tip – Handle Edge Cases
When working with lists in Python, it’s always a good idea to handle edge cases. Here are a few things to keep in mind:
def get_last_n_safe(data_list, n):
if not data_list:
return []
if n > len(data_list):
return data_list
return data_list[-n:]
# Test cases
print(get_last_n_safe([], 3)) # Empty list
print(get_last_n_safe([1, 2, 3], 5)) # n greater than list length
print(get_last_n_safe([10, 20, 30], 2)) # Normal caseThis approach ensures your code doesn’t break when the input list is empty or smaller than n. It’s a best practice I follow in all my Python projects.
Performance Comparison
Below is a quick comparison of the methods discussed above based on speed and readability:
| Method | Speed | Readability | Best Use Case |
|---|---|---|---|
| Slicing | ✅ Fast | ✅ High | General use |
| Negative Indexing | ⚙️ Medium | ⚙️ Moderate | Custom logic |
| itertools.islice | ✅ Fast | ⚙️ Moderate | Large iterables |
| collections.deque | ✅ Fast | ✅ High | Real-time data |
| Custom Function | ✅ Fast | ✅ High | Reusable code |
| reversed() | ⚙️ Medium | ✅ High | Reverse order |
| List Comprehension | ⚙️ Medium | ✅ High | Conditional logic |
In most scenarios, list slicing or collections.deque are the best options for performance and simplicity.
Working with lists is one of the most common tasks in Python, and knowing how to efficiently get the last N elements can make your code cleaner and faster.
I personally prefer the slicing method for simplicity, but if you’re handling real-time data or large streams, deque or itertools.islice might be the better choice.
You may also read:
- Iterate through a Dictionary in Python with an Index
- Convert Python Dictionary to Pandas DataFrame
- Read a CSV into a Dictionary using Pandas in Python
- Python Dictionary of Sets

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.