Python get last element of list

Lists are one of the most commonly used data structures in python programs. In this article, we will look at different ways to get the last element of a list in python. For this, we will use ways like indexing, pop() method, slicing and reverse iterator. 

Get the last element of a list using indexing in Python

Indexing in python is a way to access elements from a list. In python, we can use positive indices as well as negative indices. Positive indices start with zero which corresponds to the first element of the list and the last element of the list is identified by the index “listLen-1” where “listLen” is the length of the list.

Alternatively, negative indices start from -1 which corresponds to the last element of the list. It goes till “-listLen” where listLen is the length of the list. The index “-listLen” corresponds to the first element in the list.

Python get last element of list

To get the last element of a list, we can first find the length of the list using the len() function. Then we can access the last element of the list at the index “listLen-1” as follows.

myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) listLen = len(myList) lastElement = myList[listLen - 1] print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

Alternatively, we can use negative indexing to access the last element of the list. The last element of the list is at index -1 which can be accessed as follows.

myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) lastElement = myList[- 1] print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

We can see that while using negative indexing, we don’t have to calculate the length of the list.

Using the pop() method

The pop() method is used to remove any element from the list from a specified index. It takes the index of the element as an optional input parameter and returns the element at the specified index after deleting it from the list. If no input parameter is passed, it will return the last element of the list after deleting it.

We can use the pop() method to get the last element of the list as follows.

myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) lastElement = myList.pop() print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

Remember that the pop() method also deletes the element which is accessed. So, Use this method only when you also want to delete the last element of the list.

Get the last element of a list using Slicing in Python

In python, slicing is an operation to create a subpart of a string or a list. With slicing, we can access different parts of any string, tuple or a list. To perform slicing on a list named, we use the syntax listName[start, end,interval] where “start” and “end” are the indices at which the sliced list starts and ends in the original list respectively. The “interval” is used to select elements in a sequence. The elements are selected from the list at the indices which are whole number multiples of “interval” away from the start index. 

To access the last element of a list using slicing in python, we can slice a list in such a way that it contains only the last element. Then we can access that element as follows.

myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) lastElement = myList[-1:][0] print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

Get the last element of a list using reverse iterator

We can use a reverse iterator to get the last element of the list. To create a reverse iterator, we can use the reversed() method. The reversed() method takes any iterable object as input and returns a reverse iterator of the iterable.

To get the last element of a list, we will first create a reverse iterator of the list using the reversed() method. Then we will access the first element of the reverse iterator which will be the last element of the original list. This can be done as follows.

myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) reverseIter = reversed(myList) lastElement = next(reverseIter) print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

Get the last element of a list using itemgetter

We can create an itemgetter object to access the last element of a list. The itemgetter() method is defined in the operator module in python. The itemgetter() method takes the index as input which creates a callable object. The callable object takes an iterable as input and extracts the element at the specified index. 

To access the last element of the list, we can call itemgetter() method with input index as -1 and then  we can access the last element of the list as follows.

import operator myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) lastElement = operator.itemgetter(-1)(myList) print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

Conclusion

In this article, we have seen different ways to get the last element of a list in python. To read more about lists, read this article on list comprehension in python. Stay tuned for more informative articles.

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

In this article, we will discuss six different ways to get the last element of a list in python.

Get last item of a list using negative indexing

List in python supports negative indexing. So, if we have a list of size “S”, then to access the Nth element from last we can use the index “-N”. Let’s understand by an example, Suppose we have a list of size 10,

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] To access the last element i.e. element at index 9 we can use the index -1,# Get last element by accessing element at index -1 last_elem = sample_list[-1] print('Last Element: ', last_elem) Output:Last Element: 9 Similarly, to access the second last element i.e. at index 8 we can use the index -2.last_elem = sample_list[-2] Output:Last Element: 8
Using negative indexing, you can select elements from the end of list, it is a very efficient solution even if you list is of very large size. Also, this is the most simplest and most used solution to get the last element of list. Let’s discuss some other ways,

Get last item of a list using list.pop()

In python, list class provides a function pop(),

list.pop(index)
It accepts an optional argument i.e. an index position and removes the item at the given index position and returns that. Whereas, if no argument is provided in the pop() function, then the default value of index is considered as -1. It means if the pop() function is called without any argument then it removes the last item of list and returns that.

Let’s use this to remove and get the last item of the list,

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Remove and returns the last item of list last_elem = sample_list.pop() print('Last Element: ', last_elem) Output:

Last Element: 9
The main difference between this approach and previous one is that, in addition to returning the last element of list, it also removes that from the list.

Get last item of a list by slicing

We can slice the end of list and then select first item from it,

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Get a Slice of list, that contains only last item and select that item last_elem = sample_list[-1:][0] print('Last Element: ', last_elem) Output:

Last Element: 9
We created a slice of list that contains only the last item of list and then we selected the first item from that sliced list. It gives us the last item of list. Although it is the most inefficient approach, it is always good to know different options.

Get last item of a list using itemgetter

Python’s operator module provides a function,

operator.itemgetter(item) It returns a callable object that fetches items from its operand using the operand’s __getitem__() method. Let’s use this to get the last item of list by passing list as an operand and index position -1 as item to be fetched.

import operator sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] last_elem = operator.itemgetter(-1)(sample_list) print('Last Element: ', last_elem) Output:Last Element: 9
It gives us the last item of list.

Get last item of a list through Reverse Iterator

In this solution we are going to use two built-in functions,

  1. reversed() function : It accepts a sequence and returns a Reverse Iterator of that sequence.
  2. next() function: It accepts an iterator and returns the next item from the iterator.

So, let’s use both the reversed() and next() function to get the last item of a list,

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # get Reverse Iterator and fetch first element from reverse direction last_elem = next(reversed(sample_list), None) print('Last Element: ', last_elem) Output:

Last Element: 9 It gives us the last item of list.

How did it work?


By calling the reversed() function we got a Reverse Iterator and then we passed this Reverse Iterator to the next() function. Which returned the next item from the iterator.
As it was a Reverse Iterator of our list sequence, so it returned the first item in reverse order i.e. last element of the list.

Get last item of a list by indexing

As the indexing in a list starts from 0th index. So, if our list is of size S, then we can get the last element of list by selecting item at index position S-1. Let’s understand this by an example,

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # get element at index position size-1 last_elem = sample_list[len(sample_list) - 1] print('Last Element: ', last_elem) Output:Last Element: 9
It gives us the last item of list.

Using the len() function we got the size of the list and then by selecting the item at index position size-1, we fetched the last item of the list.

So, here we discussed 6 different ways to fetch the last element of a list, although first solution is the simplest, efficient and most used solution. But it is always good to know other options, it gives you exposure to different features of language. It might be possible that in future, you might encounter any situation where you need to use something else, like in 2nd example we deleted the last element too after fetching its value.

Happy Coding.

The Complete example is as follows,

import operator def main(): print('*** Get last item of a list using negative indexing ***') sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Get last element by accessing element at index -1 last_elem = sample_list[-1] print('Last Element: ', last_elem) print('*** Get last item of a list using list.pop() ***') sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Remove and returns the last item of list last_elem = sample_list.pop() print('Last Element: ', last_elem) print('*** Get last item of a list by slicing ***') sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] last_elem = sample_list[-1:][0] print('Last Element: ', last_elem) print('*** Get last item of a list using itemgetter ***') sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] last_elem = operator.itemgetter(-1)(sample_list) print('Last Element: ', last_elem) print('*** Get last item of a list through Reverse Iterator ***') sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # get Reverse Iterator and fetch first element from reverse direction last_elem = next(reversed(sample_list), None) print('Last Element: ', last_elem) print("*** Get last item of a list by indexing ***") sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # get element at index position size-1 last_elem = sample_list[len(sample_list) - 1] print('Last Element: ', last_elem) if __name__ == '__main__': main() Output:

*** Get last item of a list using negative indexing *** Last Element: 9 *** Get last item of a list using list.pop() *** Last Element: 9 *** Get last item of a list by slicing *** Last Element: 9 *** Get last item of a list using itemgetter *** Last Element: 9 *** Get last item of a list through Reverse Iterator *** Last Element: 9 *** Get last item of a list by indexing *** Last Element: 9