How to Get the Last Element in a List in Python

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]
Command Prompt output of Python negative indexing returning the last and second to last items of a list and a two-item slice
Both [-1] and len(orders) – 1 return 1004, [-2] gives 1003, and [-2:] gives the last two as a list.

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
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
Indexing the empty list fails, the conditional gives None, the slice returns an empty list, and the fallback prints no orders yet.

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
Command Prompt output of Python finding the last checkout event in a log with next and reversed, and None when no refund exists
The last checkout is Emily’s at 10:31, and the search for a refund returns None instead of raising.

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
Command Prompt output of Python getting the last sale of each store from nested lists, and the last value of a generator with deque
Each store’s last sale is listed, the very last value is 310.0, and a generator raises TypeError until deque(maxlen=1) grabs its last reading.

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

GoalCodeEmpty list
Read the last itemlst[-1]IndexError
Read with a defaultlst[-1] if lst else NoneNone
Remove and returnlst.pop()IndexError
Last N itemslst[-n:][]
Last matchnext((x for x in reversed(lst) if ...), None)None
Last of a generatordeque(gen, maxlen=1)[0]IndexError

These go well with what you just learned:

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.