Python Get Last Element in List

Are you looking for a way to get the last element of the list in Python? In this Python tutorial, we will teach you multiple ways to access the last element of the list in Python.

While working on an E-commerce website project using Python, I needed to access the last product added to the cart to display it as the most recently added product.

In this type of situation, you have to access the last element of the list in Python.

How to Get the Last Element from the List in Python

Let’s understand the scenario first, so you will understand where you should use these approaches in the real-time project,

cart = ["watch", "earbuds", "belt", "perfume"]

So these are the products that the user has added to the cart; now, I need to show the message to the user that the most recent product was added to the cart, which is the last element of the cart.

The output should look like this,

Most Recenet Product you've added is: Perfume

Let’s see every method one by one with practical examples to get the last element in Python.

Python Get Last Element in List by Indexing

First we will use indexing to get the last element of the list by giving their index positions in Python. The List in Python is an ordered collection, which means it contains their index position in sequence from 0 – len(list)-1.

We can access the element of the list by giving indexing in two ways: Negative Indexing and Positive Indexing (with the help of the len() method) in Python.

READ:  How to Split Last Element in Python

Let’s see both the approach individually with practical examples.

Python Get Last Element in List by Negative Indexing

We can use Negative Indexing to access the last element of the List or other ordered collections in Python. Negative Indexing is also the sequence of the element’s position, which starts from the last element.

Negative Indexing is the preferable approach to targeting the last element because it is the easiest and most direct way to do so in any ordered collection.

Syntax of Negative Index in Python

list_name[-1]
  • list_name[-1]: Here, it will target the last element of the list directly.
cart = ["watch", "earbuds", "belt", "perfume"]

last_element = cart[-1]

print("Most recent product added is: "+ last_element)
Python Get Last Element in List by Negative Indexing

In the above code, we have a list named cart where users have added some products into the cart, and we need to display the most recent product added to the cart, which is the last element of the list.

So we are accessing the last element using negative indexing like this last_element = cart[-1], so it will extract the last element to the last_element variable.

Python Get Last Element of List using len() method in Python

If we need to get the last element of the list using the positive indexing, then we have to use the len() method to get the last index. Generally, the len() method is used to count the number of elements existing in the list.

So, we will create a logic where we will get the last element of the list with the positive indexing in Python.

Syntax

list[len(list)-1]
  • len(list)-1: This part from the syntax will return the positive index position of the last element.

Let’s understand with a practical example of How Python Get Last Element in List

cart = ["watch", "earbuds", "belt", "perfume"]

last_element = cart[len(cart)-1]

print(last_element)
Python Get Last Element of List using len() method in Python

In the above code, we are using the same example we took in the previous one but with a different approach. Here, we are using the len() method to get the count of elements in the list, and indexing in Python starts from 0, so the last element is always the len(list) – 1.

READ:  How to Drop Rows in Python Pandas DataFrames [4 Examples]

So we extract the last element from the list in Python like this, last_element = cart[len(cart)-1].

How to Get Python Last Element in List Using pop() Method

We can also use the pop() method to extract the last element from the list in Python. Generally, the pop() method in Python is used to remove the last element from the collections; that’s why we’ve also used the copy method to create the copy of the original list.

Syntax

list.pop()
  • list.pop(): By default, it will remove the last element from the list if you don’t give any parameter. You can give index position as a parameter to the pop() like this list.pop(1).

Let’s see How Python Get Last Element in List using the pop() method

emp_id = [1001, 1002, 1003, 1004, 1005, 1006]

data = emp_id.copy()

last_element = data.pop()

print("Last ID is:",last_element)

print("Updated List: ",emp_id)
How to Get Python Last Element in List Using pop() Method

In the above code, we have a list of emp_id’s, and we need to check which is the last employee we’ve added to the list.

So first, we created a copy of that collection because if we use the pop() method to the original collection directly, it will make changes in the original list; that’s why we created a copy of the list using the copy() method like this: data = emp_id.copy().

Then, we extract the last element from the data, which is the copy of our original list emp_id.

Get Python last element of List using generators and reversed() method in Python

We can also use generators in Python, which iterate over elements one by one without using looping. We’ve also used the reversed() method to reverse the list in Python.

After reversing the list, we will start the iteration using the next() method. We will iterate only one time because we need to extract the first element of the reversed list.

READ:  Python Tkinter Listbox - How to Use

Syntax

next(reversed(list_name))
  • From the syntax, reversed(list_name) this part will execute first to reverse the list. Then next(reversed(list_name)), next() will execute to iterate to the first element.
client_names = ["Arthur", "Alfred", "Albert", "William", "Henry"]
last_element = next(reversed(client_names))
print(last_element)
Get Python last element of List using generators and reversed() method in Python

In the above code, we have a list of client names, and I need to get the last client name from the list.

So, we reversed the list using the reversed() method, like this: reversed(client_names). It will iterate to the first element of the reversed list like this: next(reversed(client_names)), which is the last element of the original list.

How to Extract the Last Element of a List in Python using the itemgetter() method

We can use the itemgetter() method, a built-in function of the operator module in Python. It takes the index position as a parameter with the collection name in the parentheses.

Syntax

var_name = itemgetter(-1)(list_name)
  • itemgetter(-1)(list_name): Before using this method, you have to import this function like this, “from operator import itemgetter”.

Let’s understand how Python get the last element in the list using the item getter () method.

from operator import itemgetter

emp_data = [1001, 1002, 1003, 1004, 1005, 1006]
last_element = itemgetter(-1)(emp_data)
print("Last Employee Id is:",last_element)
How to Extract the Last Element of a List in Python using the itemgetter() method

In the above code, we have a list of employee IDs and need to extract the last element of the list. So we are using the itemgetter() method like this: itemgetter(-1)(emp_data), where emp_data is my list name, and -1 is a negative index of the list.

Conclusion

In this Python article, you learned How Python Get the Last Element in a List in multiple ways with practical examples.

We used negative indexing and the len() method to get the last element’s positive index position, and we used the pop() method, generator, and itemgetter() method to get the last element from the list in Python.

You may like to read: