To get the last element of a list in Python, use the index -1: orders[-1]. Negative indexes count from the end, so -2 is the second to last. On an empty list [-1] raises IndexError, so check first when the list might be empty:
last = orders[-1] if orders else None
The examples use order numbers and an event log; their output is from Python 3.12.5, including the two errors you’re most likely to hit.
You’ll see [-1] and its relatives, empty lists, peeking vs pop(), the last item that matches a condition, nested lists, and generators that can’t be indexed.
Get the last element of a list with [-1]
Negative indexing is the idiomatic way. len(lst) - 1 works too but is longer and easier to get wrong:
orders = [1001, 1002, 1003, 1004]
print(orders[-1]) # last
print(orders[len(orders) - 1]) # same thing, longer
print(orders[-2]) # second to last
print(orders[-2:]) # last two, as a list
Output:
1004
1004
1003
[1003, 1004]

A slice like [-2:] returns a list rather than an item. For any number of trailing items, see getting the last N elements of a list.
Why does [-1] raise IndexError on an empty list?
An empty list has no last item, so Python raises IndexError: list index out of range. Use a conditional expression, or a slice, which never raises:
orders = []
try:
print(orders[-1])
except IndexError as err:
print("IndexError:", err)
last = orders[-1] if orders else None # safe default
print("last:", last)
print(orders[-1:], "<- slicing never raises")
print((orders[-1:] or ["no orders yet"])[0])
Output:
IndexError: list index out of range
last: None
[] <- slicing never raises
no orders yet
![python last element empty list indexerror safe default command prompt Command Prompt output of Python IndexError list index out of range for [-1] on an empty list and safe defaults with a condition and a slice](https://pythonguides.com/wp-content/uploads/2026/10/python-last-element-empty-list-indexerror-safe-default-command-prompt.png)
I use the conditional form; it’s the clearest. The general error is covered in fixing IndexError: list index out of range.
Get the last element with or without removing it
[-1] only reads the item. pop() returns it and removes it from the list, which is what you want for stacks and queues:
queue = ["Emily", "James", "Olivia"]
peek = queue[-1] # look, don't touch
print(peek, queue)
taken = queue.pop() # remove and return the last item
print(taken, queue)
Output:
Olivia ['Emily', 'James', 'Olivia']
Olivia ['Emily', 'James']
Both are fast because nothing shifts. More ways to drop the end are in removing the last element from a list.
Get the last element that matches a condition
Walk the list backward with reversed() and stop at the first match. next() with a default handles the no-match case:
events = [
("2026-10-06 09:12", "login", "emily"),
("2026-10-06 09:40", "checkout", "james"),
("2026-10-06 10:05", "login", "olivia"),
("2026-10-06 10:31", "checkout", "emily"),
("2026-10-06 11:02", "logout", "olivia"),
]
last_checkout = next((e for e in reversed(events) if e[1] == "checkout"), None)
print("last checkout:", last_checkout)
last_refund = next((e for e in reversed(events) if e[1] == "refund"), None)
print("last refund: ", last_refund)
Output:
last checkout: ('2026-10-06 10:31', 'checkout', 'emily')
last refund: None

reversed() doesn’t copy the list, so this is cheap even on long logs. Reading the last line of a file is a similar task, covered in reading the last line of a file.
Get the last element of nested lists and generators
For a list of lists, index each row. Generators can’t be indexed, so let a deque with maxlen=1 keep only the final value:
from collections import deque
sales = [[1250.50, 980.25, 1120.75], [640.00, 1499.99], [310.00]]
print("last sale of each store:", [row[-1] for row in sales])
print("very last value: ", sales[-1][-1])
readings = (x * 1.5 for x in range(1, 6)) # a generator has no [-1]
try:
readings[-1]
except TypeError as err:
print("TypeError:", err)
print("last reading:", deque(readings, maxlen=1)[0])
Output:
last sale of each store: [1120.75, 1499.99, 310.0]
very last value: 310.0
TypeError: 'generator' object is not subscriptable
last reading: 7.5

The deque walks the generator once and keeps memory flat. For the sizes of nested lists, see finding the size of a list in Python.
Last element methods compared
| Goal | Code | Empty list |
|---|---|---|
| Read the last item | lst[-1] | IndexError |
| Read with a default | lst[-1] if lst else None | None |
| Remove and return | lst.pop() | IndexError |
| Last N items | lst[-n:] | [] |
| Last match | next((x for x in reversed(lst) if ...), None) | None |
| Last of a generator | deque(gen, maxlen=1)[0] | IndexError |
These go well with what you just learned:
- Get the first element of a tuple
- Replace items in a Python list
- Shuffle a list in Python
- Get the last 3 characters of a string
Frequently asked questions
How do I get the last element of a list in Python?
Use my_list[-1].
How do I get the last element without an error on empty lists?
Use my_list[-1] if my_list else None, or my_list[-1:] for a list that may be empty.
What’s the difference between [-1] and pop()?
[-1] reads the item; pop() also removes it from the list.
How do I get the second to last element?
Use my_list[-2].
How do I get the last item of a generator?
Use collections.deque(gen, maxlen=1)[0], which consumes it while keeping only the last value.
Where is deque documented?
See collections.deque in the Python docs.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn