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.
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:
Output:
This is the normal Python answer because it is concise, readable, and built into the language.
The general rule is:
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:
Output:
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:
Output:
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:
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:
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:
Output:
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:
That does not mean “last two items.” It means “everything except the last two items.”
Example:
Output:
So the placement of the minus sign and colon matters a lot.
Useful Patterns to Remember
Last item:
Last three items:
All but the last item:
Reverse the list:
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 lastnitems of a Python list. - Use
values[-1]when you want the final single item. - Slicing is safe when
nis 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
- how to get longest repeating string in substring from suffix tree
- How to get Spring RabbitMQ to create a new Queue?
- How to get the caller's method name in the called method?
- How to get the Cartesian product of multiple lists
- How to get Linux console window width in Python
- How to get list of points inside a polygon in python?
- How to get the cut-set using the Edmonds–Karp algorithm?
- How to get the difference between two arrays in JavaScript?

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 courseTrack 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.