How to Get the Last Item of a List in Python: Iterable Unpacking and More

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

Last Updated on

Welcome back to another edition of the How to Python 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 other words, we want to access the item at the last index 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?

Fortunately, Python a lot of tools for interacting with lists. Let’s see if we can find the best solution.

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.

The main drawback with a solution like this is the time complexity. In order to access the last element, we have to compute some expression which involves a function call and some arithmetic. Fortunately, 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 How to Python 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.

Performance

As always, I like to start performance testing by storing all of our solutions in strings:

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

length = """
last_item = my_list[len(my_list) - 1]
"""

pop = """
last_item = my_list.pop()
my_list.append(last_item)
"""

negative_index = """
last_item = my_list[-1]
"""

iterable_unpacking = """
*_, last_item = my_list
"""

Unfortunately, the pop solution makes performance testing difficult because it changes our underlying list. As a result, I have to add the item back during the test. This may alter the performance considerably.

At any rate, we can test each solution using the timeit library:

>>> min(timeit.repeat(stmt=length, setup=setup, repeat=10))
0.08844650000003185
>>> min(timeit.repeat(stmt=pop, setup=setup, repeat=10))
0.109169100000031
>>> min(timeit.repeat(stmt=negative_index, setup=setup, repeat=10))
0.03161860000000161
>>> min(timeit.repeat(stmt=iterable_unpacking, setup=setup, repeat=10))
0.13539270000001125

As it turns out, negative indexing is incredibly fast. I assume that’s because mod and random access to the list are both constant time operations.

Surprisingly, iterable unpacking appears to be quite slow. In addition, I think the syntax is hard to read, but I love how clever it is. That said, if I had to choose a method, I’d definitely go the negative indexing route. Ultimately, the choice is yours, however.

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 & quickest 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 Python: Slice, Copy, and MoreHow to Make a Python Script Shortcut with Arguments: Batch, Bash, and More →
Advertisements

Leave a Comment

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