Python
list slicing
programming tutorial
Python tips
Python lists

How to get last items of a list in Python?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In Python, the simplest way to get the last items of a list is slicing with a negative start index. That covers most use cases cleanly, but there are a few related patterns worth knowing: getting the last single item, handling lists shorter than n, and doing the same thing for general iterables rather than real lists.

The Standard Solution: Negative Slicing

To get the last n elements of a list, use:

python
values = [10, 20, 30, 40, 50]
last_two = values[-2:]
print(last_two)

Output:

python
[40, 50]

This is the normal Python answer because it is concise, readable, and built into the language.

The general rule is:

python
values[-n:]

That means “slice from the nth item from the end up to the end of the list.”

Get the Last Single Item

If you need only the final element, index it directly:

python
values = [10, 20, 30, 40, 50]
print(values[-1])

Output:

python
50

This is different from slicing:

  • 'values[-1] returns one element'
  • 'values[-1:] returns a one-element list'

That distinction matters when later code expects a scalar versus a list.

What Happens If n Is Larger Than the List

Slicing is forgiving. If the list is shorter than n, Python just returns the whole list:

python
values = [1, 2, 3]
print(values[-10:])

Output:

python
[1, 2, 3]

This is one reason slicing is so convenient. You usually do not need extra length checks.

Direct indexing does not behave the same way. This fails on an empty list:

python
values = []
print(values[-1])

That raises IndexError, so handle empty lists explicitly when you need a single last item.

Copy Versus View

In Python lists, slicing creates a new list object:

python
1values = [10, 20, 30, 40, 50]
2tail = values[-2:]
3
4tail[0] = 999
5print(values)
6print(tail)

values remains unchanged because tail is a separate list. That is usually what you want, but it is worth remembering if you are working with large lists and care about extra allocation.

If You Need the Last Items From Any Iterable

Slicing works only on sequence types that support indexing. If you have a generator or a streaming iterable, use collections.deque:

python
1from collections import deque
2
3numbers = (x for x in range(10))
4last_three = list(deque(numbers, maxlen=3))
5print(last_three)

Output:

python
[7, 8, 9]

This is a strong pattern when you are processing input lazily and do not want to convert the entire iterable into a list first.

Reverse Slicing Is a Different Operation

Sometimes people accidentally write:

python
values[:-2]

That does not mean “last two items.” It means “everything except the last two items.”

Example:

python
values = [10, 20, 30, 40, 50]
print(values[:-2])

Output:

python
[10, 20, 30]

So the placement of the minus sign and colon matters a lot.

Useful Patterns to Remember

Last item:

python
values[-1]

Last three items:

python
values[-3:]

All but the last item:

python
values[:-1]

Reverse the list:

python
values[::-1]

These are all related slice patterns, and once you get comfortable with them, list-tail operations become second nature.

Common Pitfalls

The biggest pitfall is confusing values[-n:] with values[:-n]. The first gives the last n elements; the second drops them.

Another common issue is expecting values[-1] to work on an empty list. Direct indexing raises an exception there, while slicing returns an empty list.

People also sometimes forget that slicing returns a new list rather than a view into the original list. That is usually fine, but it matters if the list is large or if you expected in-place behavior.

Finally, slicing works for lists and similar sequence types, but not for all iterables. For generators and streams, use a deque with maxlen.

Summary

  • Use values[-n:] to get the last n items of a Python list.
  • Use values[-1] when you want the final single item.
  • Slicing is safe when n is larger than the list length.
  • Slicing returns a new list, not a view.
  • For general iterables and streams, collections.deque(..., maxlen=n) is the right tail pattern.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.