How to Get the Last N Elements of a List in Python

A shop owner asked me for a dashboard tile showing only the most recent days of sales. That’s the classic job to get the last N elements of a list in Python.

You could loop with an index counter, but that’s five lines for a one-line job, and it breaks once n outgrows the list.

Quick answer: slice with a negative start. items[-n:] returns a new list holding the last n items, in their original order:

last_three = sales[-3:]   # last 3 items, oldest first

In this tutorial, I’ll cover that slice, the one value of n where it lies to you, and what to use when your data isn’t a list. You’ll see real outputs from Python 3.12.5 on Windows.

How do you get the last N elements of a list with slicing?

The running sample is daily_sales.py: ten days of store sales in USD, October 20-29, 2026.

from daily_sales import sales

n = 3
last_three = sales[-n:]
print("Last 3 days:", last_three)
print(f"Total: ${sum(last_three):,.2f}")

Output:

Last 3 days: [1388.6, 1902.35, 2214.8]
Total: $5,505.75
Command Prompt output of Python sales[-3:] returning the last three daily sales totals and their sum
sales[-3:] returns the last three days, totaling $5,505.75.

A negative start counts from the end, and leaving out the stop means “run to the end”.

The slice is a copy. My guide on how to slice lists in Python covers start, stop and step in depth.

Why does items[-0:] return the whole list?

This bit me on a client report: when n was 0, the tile showed every day.

-0 is just 0, so sales[-0:] means sales[0:]. A tiny guard fixes it:

from daily_sales import sales

def last_n(items, n):
    return items[-n:] if n > 0 else []

n = 0
print("sales[-0:] ->", len(sales[-n:]), "items")
print("last_n(sales, 0) ->", last_n(sales, 0))
print("sales[-50:] ->", len(sales[-50:]), "items, no IndexError")

Output:

sales[-0:] -> 10 items
last_n(sales, 0) -> []
sales[-50:] -> 10 items, no IndexError
Command Prompt output showing Python sales[-0:] returning all 10 items while the last_n helper returns an empty list
sales[-0:] returns all 10 items, the helper returns [], and sales[-50:] returns all 10.

An oversized n never raises; indexing does, as covered in fixing IndexError: list index out of range.

Get the last N lines of a file or generator with deque(maxlen=n)

A file or generator can’t be sliced, so I feed it to collections.deque with maxlen:

from collections import deque

with open("sales_log.csv", encoding="utf-8") as f:
    last_lines = deque(f, maxlen=3)

for line in last_lines:
    print(line.strip())

squares = (x * x for x in range(1, 1_000_001))
print(list(deque(squares, maxlen=2)))

Output:

2026-10-27,1388.60
2026-10-28,1902.35
2026-10-29,2214.80
[999998000001, 1000000000000]
Command Prompt output of Python deque with maxlen=3 printing the last three lines of a sales CSV file
deque(f, maxlen=3) keeps the last three CSV rows; the generator gives its last two squares.

Once full, a deque drops its oldest item as each new one arrives, so you only hold n items.

See reading a file line by line in Python, or reading the last line of a file for one line.

Get the last N elements in reverse order or remove them

Same sales list, now newest day first, then trimmed in place:

from daily_sales import sales

n = 3
print("Newest first:", sales[:-n - 1:-1])
print("Same thing:  ", sales[-n:][::-1])

week = sales.copy()
del week[-n:]
print("After del week[-3:]:", len(week), "days left")

Output:

Newest first: [2214.8, 1902.35, 1388.6]
Same thing:   [2214.8, 1902.35, 1388.6]
After del week[-3:]: 7 days left

sales[:-n - 1:-1] reads awkwardly, so I write sales[-n:][::-1]. Both give the same three values.

del week[-3:] leaves 7 days. For one item, see removing the last element from a list.

Common errors when slicing the last N items

from daily_sales import sales

n = len(sales) / 2
print("n =", n)
try:
    print(sales[-n:])
except TypeError as err:
    print("TypeError:", err)
print(sales[-int(n):])

gen = (s for s in sales)
try:
    print(gen[-2:])
except TypeError as err:
    print("TypeError:", err)

Output:

n = 5.0
TypeError: slice indices must be integers or None or have an __index__ method
[1675.4, 2030.1, 1388.6, 1902.35, 2214.8]
TypeError: 'generator' object is not subscriptable
Command Prompt output of Python TypeError slice indices must be integers and generator object is not subscriptable
A float n and a sliced generator each raise a TypeError.

len(sales) / 2 gives the float 5.0, so use // or int(). For generators, you’ll want the deque trick above.

Ways to get the last N elements compared

ApproachWorks onWhen I use it
items[-n:]Lists, tuples, stringsDefault choice
items[-n:] if n > 0 else []SameWhen n can be 0
deque(it, maxlen=n)Files, generators, any iterableNo length, or huge input
items[-n:][::-1]SequencesNewest first
del items[-n:]ListsRemove them in place

Strings slice the same way, as in getting the last 3 characters of a string.

Working with list positions? These pair well:

Frequently asked questions

How do I get the last N elements of a list in Python?

Use items[-n:]. It returns a new list of the last n items, in order.

Why does list[-0:] return the entire list?

-0 equals 0, so the slice starts at the beginning. Guard it with items[-n:] if n > 0 else [].

What happens if N is larger than the list?

The slice returns every item, with no IndexError.

Does slicing the last N elements copy the list?

Yes. Changing the slice won’t affect the original list.

How do I get the last N items of a generator?

Use collections.deque(gen, maxlen=n), which keeps only the last n.

Where is deque documented?

See collections.deque in the Python docs.