Last Element in List Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

List in Python is a data structure that can be used to store data in sequential order. Each element has an index starting from 0. Accessing the last element of a list in Python can be tricky. There are multiple ways to do so. It is essential to know all the methods. Depending on requirements, we can choose the best method among all the methods that are provided by Python.

How to Get the Last Element in a List in Python?

Introduction

A list in Python is represented using a square bracket. Each element in the list has an index, with the first element having an index starting from 0.

Example:

Output:

Problem Statement:

Given a non-empty list of elements, the problem is to print the last element of the list in Python.

Example:

Input:

Output:

Input:

Output:

Different Ways to Get the Last Element in a List in Python

There are many ways to extract the last element in a list in Python. Some of them are:

  • Using Reverse Iterator
  • Using Negative Indexing
  • Using the in-built function list.pop()
  • Using in-built functions reversed() + next()
  • Using slicing
  • Using itemgetter

Some methods are better and more convenient than others. The important thing is to know all these methods and how they work.

Using Reverse Iterator to Get the Last Element in a List in Python

We can get the last element of a list in Python by reversing the list and accessing the first element of the list.

Code:

Output:

This method is inefficient as it reverses the whole list, to extract the last element of a list in Python. For a list of large sizes, this method is not preferred.

Using Negative Indexing to Get the Last Element in a List in Python

We know that each element in the list has an index, which can be used to access the elements of the list using square brackets.

In Python, we can also use negative values to access the elements from the list. While the positive index starts from 0, which denotes the first element of the list, the negative index starts from -1 and denotes the last element of the list in Python.

Essentially, the Python interpreter converts these negative numbers into positive ones by adding the length of the list to these values.

Example:

Suppose we are given a list L=[1,2,3,4,5]L = [1, 2, 3, 4, 5]. The length of this list is 5. If we try to access L[1]L[-1], the Python interpreter will interpret this as L[1+5]=L[4]=5L[-1 + 5] = L[4] = 5. In this way, we can get the last element of this list.

Code:

Output:

Using negative indexing is a very efficient solution to get the last element from the list, even if the size of the list is large. The list[-n] gives the nth-to-last element of the list.

Using list.pop() to Get the Last Element in a Python List

In Python, the list class provides a method pop().

This function accepts an optional argument index. This function removes and returns the elements at the specified index from the list. If no value is provided, the default value is -1. It means that, by default, the pop() function returns the last element in the list in Python.

Code:

Output:

As we see from the example, the last element is removed from the list. Use this method, only when you want to delete the last element in the list in Python.

Using reversed() + next() to Get the Last Element in a List in Python

To get the last element in a list in Python, we can reverse the list and print the first element from the list. To perform these operations, we use two in-built functions:

  • reversed():
    This function reverses the list and returns the iterator to the first element of the reversed list.
  • next():
    This function accepts an iterator and returns the next element of the iterator.

Code:

Output:

This method can be used to access the last element in O(1) time and can be beneficial if the logic of the program requires iterating the list from the end.

Using Slicing to Get the Last Element in a Python List

Python provides a slicing operator for the list data structure. We can slice any part of the list using the following syntax.

Here startIndex is the starting index of the element from where we want to slice the list and endIndex is the index of the ending element of the slice.

Example:

Output:

We can use this operator to slice the last element in the list in Python. This new sliced list will contain only one element which is the last element of the original list.

Slicing creates a copy of the original list, hence this is the most inefficient method to access the last element of the list in Python.

Code:

Output:

Using "itemgetter" to Get the Last Element in a List in Python

Python's operator module provides a method itemgetter to conveniently access the elements of an iterable object (list, tuple, set).

This function constructs a callable that assumes an iterable object as input and fetches the n-th element out of it.

We can use this function to get the last element in the list in Python by creating a callable object using the index as -1. Now, we can call the newly created object using the list as a parameter to get the last element.

Code:

Output:

The method itemgetter is preferred over lambda functions as it is more time efficient. This method is beneficial as it can create a callable object which can return the last element of any container in Python.

Which is the Best Way to Get the Last Element in a Python List?

Among all the methods described above, if we do not want to change the list, the best method is to use the negative index -1. This method is convenient, concise, and efficient.

If we want to delete the last element of the list in Python, then the list.pop() method should be preferred.

If we want to get an iterator to the list from the end, then we can use the reversed() + next() method to access elements in reverse order from the list.

Depending on the requirements, different methods are useful to access the last element of the list.

Conclusion

  • The last element in the list in Python can be accessed by passing the value -1 as an index. This is the most used method to access the last element.
  • list.pop() removes and returns the last element in the list in Python.
  • reversed() returns an iterator to the reversed list. next() function can be used to access the last element of the list from the iterator.
  • Slicing is a way to copy a part of the list in Python. We can create a sliced list containing only the last element of the list.
  • itemgetter function from the module operator can create a callable object using an index as an argument. This object can be called with the list as a parameter to get the value at the specified index.