How to Get the Last Item of a List in Python

How to Get the Last Item of a List in Python Featured Image

Welcome back to another edition of the Python Bytes series. Last time, we covered how to clone a list in Python. Now, we’re look at how to get the last item of a list in Python.

If you checked out the cloning article, you might be a bit worried that we have another long one today. Luckily, getting the last item of a list is really simple.

Table of Contents

Problem Introduction

The problem is simple. Let’s imagine we have a list:

my_list = ['red', 'blue', 'green']

And, we want to get the last item in the list. In this case, we can see pretty clearly that the last item is ‘green,’ but how do we access it in code?

Solutions

If we want to get the last item of a list, we have a handful of options. Let’s dig into a few.

Get the Last Item Using Brute Force

As always, we like to tackle these problems using the naïve method. As someone with a Java background, my first reaction is to get the length of the list and use that to determine the last index. In Python, this would look something like the following:

my_list = ['red', 'blue', 'green']
last_item = my_list[len(my_list) - 1]
print(last_item)  # prints 'green'

Basically, we just get the length of the list and subtract that length by one. That gives us the index of the last item in the list. But what if there’s a better way?

Get the Last Item Using Pop

Another fun way to get the last item of a list is to use the pop method. If you’re unfamiliar with the various list methods, here are a few fun ones:

  • append – adds an item to the end of the list
  • pop – removes and returns the last item of the list
  • clear – removes all items from the list
  • reverse – reverses the elements of the list in place

As you can see, pop will get us our last item:

my_list = ['red', 'blue', 'green']
last_item = my_list.pop()
print(last_item)  # prints 'green'
print(my_list)  # prints ['red', 'blue']

However, we’ll actually remove that item from our list completely. If that’s the goal, this is the option for you.

Get the Last Item Using a Negative Index

For those of you familiar with languages like C/C++ and Java, this may throw you for a loop – no pun intended. In Python, you’re allowed to use negative indices, and they work exactly as you’d expect. For instance:

my_list = ['red', 'blue', 'green']
print(my_list[0])  # prints 'red'
print(my_list[-1])  # prints 'green'

Not only is this the easiest solution, it’s also the preferred solution. Most code you explore will use this negative index notation. Be aware, however, that this method will fail if the list empty.

Get the Last Item Using Iterable Unpacking

Once again, it wouldn’t be a Python Bytes tutorial if I didn’t show you at least one wild Python solution. Believe it or not, the following syntax will actually get the last item of a list:

my_list = ['red', 'blue', 'green']
*_, last_item = my_list
print(last_item)  # prints 'green'

Basically, this iterable unpacking syntax allows us to split the list into two pieces: the last item and everything else. In fact, we can actually reverse the syntax to get the first item and everything else:

my_list = ['red', 'blue', 'green']
first_item, *_ = my_list
print(first_item)  # prints 'red'

Pretty cool if you ask me. This syntax is functionally equivalent to the following:

my_list = ['red', 'blue', 'green']
first_item, *_ = my_list[0], my_list[1:]

At any rate, let’s get ready for the recap.

A Little Recap

Once again, we’re playing around with lists in this series. This time, we’re looking to get the last element of a list. Here are a few options:

my_list = ['red', 'blue', 'green']

# Get the last item with brute force using len
last_item = my_list[len(my_list) - 1]

# Remove the last item from the list using pop
last_item = my_list.pop() 

# Get the last item using negative indices *preferred method*
last_item = my_list[-1]

# Get the last item using iterable unpacking
*_, last_item = my_list

Each of these methods will retrieve the last item of a list in Python. As always, if you have other solutions, use the comments below.

Series Navigation← How to Clone a List in PythonHow to Make a Python Script Shortcut with Arguments →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.