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 isn’t too bad.
In short, there are four ways to get the last item of a list in Python. First, you can use the length of the list to calculate the index of the last item (i.e. `my_list[len(my_list) – 1]`python). In addition, you can take advantage of the pop function (i.e. `my_list.pop()`python). Otherwise, you might opt for negative indexing (i.e. `my_list[-1]`python). Finally, you can take advantage of iterable unpacking (i.e. `*_, last_item = my_list`python).
Check out the rest of the article to learn more about the pros and cons of each solution.
Table of Contents
As always, I try to create video summaries for folks who want to see the code executed live. In this case, I’ve featured all four solutions from this article in one ten-minute video. In other words, you’ll see me live code each solution using a different example list from the one in this article.
Let’s imagine we have a list which contains a few items:
my_list = ['red', 'blue', 'green']
In this case, we’ve created a list called `my_list`python, and it contains three strings: “red”, “blue”, and “green.” Each item has a fixed index which we can use to access each item. For example, if we wanted to get “red,” we’d use the zeroth index to access it:
my_list # returns "red"
If we wanted to get the last item of this list, we could use the second index:
my_list # returns "green"
Unfortunately, two is hardcoded to our example. How do we go about writing a dynamic solution which can access the last item of the list no matter the list size? For our purposes, we’ll assume the list has at least one item.
Fortunately, Python has a lot of tools for interacting with lists. Let’s see if we can find the best solution.
If we want to get the last item of a list, we have a handful of options:
- Brute force
- Pop function
- Negative indexing
- Iterable Unpacking
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) # 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, my_list[1:]
At any rate, let’s get ready for the recap.
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`python 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.
Now that we know how to get the last item of a list, I’m wondering how the concept carries to other data structures. For example, how do we get the last character of a string?
my_string = "Challenge!" last_char(my_string) # returns "!"
Do all the solutions from above still work? Are there any new solutions? If you know the answer, share it on Twitter! Don’t forget to tag it #RenegadePython. I kicked off the discussion by sharing my solution below:
If I see your solution, I’ll be sure to give it a share.
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.
I'm embracing the virtual world and moving all of my computer science exams online. Here's why!
Amid all the chaos of my daily TODO list, I was able to find order by sorting my tasks by my values. Maybe you'll find some "value" in this tip as well.