Get Last Element From a List in Python

Get Last Element From a List in Python

We will introduce different methods of getting the last element from a list in Python.

Get the Last Element From a List in Python

When we work on applications, we have to access the last element from a given list for various reasons, and we would also want to know which methods can be best for different situations.

There are many methods to get the last elements from the list in python programming. In this tutorial, we will discuss some of them.

Let’s look at these options individually. The first method we will discuss is negative indexing.

Negative Indexing in Python

Python can support negative indexing in list programs. It is usable in both conditions when we know the length of the list or when you don’t know the length of the list.

Syntax:

list [-N] # Where N is the index number

For example, list [-1] gives the last element of the list, and list [-2] gives the second-last element of the list. Let’s go through an example in which we will create a list of languages and try to access the last element from that list.

Code:

# python
lang_list = ["python", "CSS", "java", "C++", "Groovy"]

last_element= lang_list[-1]

print("last element is:", last_element)

Output:

negative indexing in python

We easily got the last element of the groovy list. If we want to get the second last element from the same list, we can use -2 instead of -1, as shown below.

Code:

# python
lang_list = ["python", "CSS", "java", "C++", "Groovy"]

last_element= lang_list[-2]

print("last element is:", last_element)

Output:

negative indexing for second last element in python

We can access elements using negative indexing from the end of the list. Remember that if the list is empty and we try to find the last element, it will give an index error.

Let’s go through an example and check how the index error occurs.

Code:

# python
no_list = []

last_element= no_list[-1]

print(last_element)

Output:

index error in negative indexing in python

If we are working with some lists and we can get through index error, it is probably because the list is empty.

Use the pop() Function in Python

The function named pop() removes the last element of the list and is also used to get the last elements of the list. This function removes the last elements from the list and returns this element as an output.

Code:

# python
lang_list = ["python", "CSS", "java", "C++", "Groovy"]

last_element= lang_list.pop()

print("List Before Using POP")
print(lang_list)

print("last element is:", last_element)

print("List After Using POP")
print(lang_list)

Output:

pop() in python

The pop() method deletes the last element from the list and outputs it. This method can only be useful when we want to check the last element of a certain list and delete it afterward.

Iteration Method in Python

We can also find the last element of the list by the method iteration using the for loop. In this method, we also use another built-in method of Python, len(), that helps us get the length of the list.

Let’s have an example and try to get the last element from the list using the iteration method below.

Code:

# python
lang_list = ["python", "CSS", "java", "C++", "Groovy"]

for j in range (0, len(lang_list)):
    if j == (len(lang_list)-1):
        print("last_element: " , lang_list[j])

Output:

iteration method in python

We easily got the last element from the list by using the iteration method in Python.

Reversing List in Python

Reversing the list is also a method to find the last element of the list. First of all, the list is reversed using python’s built-in method reverse(), and then the first index element of the reversed list is the last element of the original list, as shown below.

Code:

# python
lang_list = ["python", "CSS", "java", "C++", "Groovy"]

lang_list.reverse()

print(' last element is :', lang_list[0])

Output:

reverse method in python

Using the reverse() method, we can reverse any list, and with the help of which the last element will be now at the start, we can use the indexing to get the last element that will be now the first element of the list.

Slicing Method in Python

Slicing is another method of finding the last element of any list. It can be used in both conditions when we know the length of the list or when we don’t know the length of the list.

# python
lang_list = ["python", "CSS", "java", "C++", "Groovy"]

print(lang_list[-1:][0])

Output:

slicing method in python

We got the last element from the list using the slicing method.

Use the itemgetter() Method in Python

Itemgetter methods can get the last element from the list. Itemgetter creates a callable that takes an iterable object (e.g., list, tuple, set) as input and returns the element you asked for.

Let’s have an example and use this method to get the last element from the list. First, we have to import the operator module, and then we can use the itemgetter function to get the list’s last element, as shown.

Code:

# python
import operator

lang_list = ["python", "CSS", "java", "C++", "Groovy"]

last_element = operator.itemgetter(-1)(lang_list)

print("last element is:", last_element)

Output:

itemgetter method in python

After importing the operator module, we can easily manipulate the list using the itemgetter method and easily get the last element from the list.

Use the len() Function in Python

len() is a function used to find the length of the list, and by using this function, we can also find the last element of the list.

Code:

# python
lang_list = ["python", "CSS", "java", "C++", "Groovy"]

last_element = lang_list[len(lang_list)-1]

print("last element is: ",last_element)

Output:

len() method in python

Many methods and built-in functions can be used to get the list’s last element. These functions can also be used in different conditions, and there can be many situations in which these functions and methods can be used to make your daily life easier.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.

LinkedIn

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python