howtouselinux

3 Ways to Find Last Element of a List In Python

Table of Contents

A list is a data structure in Python that is a mutable, ordered sequence of elements. Mutable means that you can change the items inside, while the ordered sequence is in reference to index location.

The first element in a list will always be located at index 0. Each element or value that is inside of a list is called an item. Lists are defined in Python by enclosing a comma-separated sequence of objects in square brackets ([]).

3 ways to get the last element of the list in Python.

  • Using list[-1]: last_element=list[-1]
  • Using list.pop() method: last_element=list.pop()
  • Using list indexing: last_element=list[len(list) – 1]

 

Let’s see these approaches one by one.

 Find the last element of list using list[-1] in Python

The best way to find the last element of the list in Python is using the list[-1]. The list[-n] syntax gets the nth-to-last element. So list[-1] gets the last element, list[-2] gets the second to last. The list[-1] is the most preferable, shortest, and Pythonic way to get the last element.

To create a list in Python, use the square brackets and add the items in the list separated by commas.

>>> data = []
>>> for i in range(10):
… data.append(i)

or
data = list(range(0, 10))

>>> data
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In this list, the 9 element is the last item, and to access that element, write the following code.
last_element = data[-1]

What this code will do is that it will pluck the last element and store it into the last_element, and we can print the element using the print() function.

last_element = data[-1]
print(last_element)
Output
9

 

 Find last element of list using list.pop() Method in Python

The list.pop() in the Python method removes and returns the object at the given index (passed as an argument) from a list.

data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
last_element = data.pop()
print(last_element)
Output
9

Now data=[0, 1, 2, 3, 4, 5, 6, 7, 8]

Find last element of  list using indexing in Python

As the indexing in a list starts from 0th index. So, if our list length is 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.
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# get element at index position size-1
last_elem = data[len(data) – 1]
print(‘Last Element: ‘, last_elem)

IndexError in Python List

If the expected item does not exist, then it will throw an IndexError. This means that list[-1] will raise an exception if the list is empty because an empty list can’t have the last element.

data = []
last_element = data[-1]
print(last_element)
Output
Traceback (most recent call last):
File “/Users/krunal/Desktop/code/pyt/database/app.py”, line 3, in <module>
last_element = data[-1]
IndexError: list index out of range

As we can see that we got: IndexError: list index out of range

Related:

 

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

If you meet any issues with Linux, record them with Loom and send the link to us here. We will reply ASAP.

Load WordPress Sites in as fast as 37ms!