How to get the last element of a list in python?

Consider a list list_1 = [1, 2, "Red", 4, "black"]. The last item in the list_1 is "black". This article discusses different approaches to get the last element of a list in Python.

Getting the last element of a list by using Indexing

Consider a list list_1 = [1, 2, "Red", 4, "black"]. The len(list_1)-1 will be the last index value of the list. list_1[len(list_1) -1] will give the last element("black") as ouput.

In python, negative indexing starts from the end. Hence list_1[-1] will also give the last element as output.

list_1 = [1, 2, "Red", 4, "black"]
print("list_1: ", list_1)
print("Last element: ", list_1[len(list_1) -1])
print("Last element using negative indexing: ", list_1[-1])

Output

list_1:  [1, 2, 'Red', 4, 'black']
Last element:  black
Last element using negative indexing:  black

We can also get the last item of the list, by reversing the list using the reverse() function and accessing the first item of the list. The list_1.reverse() reverses the list_1. list_1[0] access the first item of the list_1.

list_1 = [1, 2, "Red", 4, "black"]
print("list_1: ", list_1)
list_1.reverse()
print("Reversed list: ", list_1)
print("Last element: ", list_1[0])

The above code returns the output as

list_1:  [1, 2, 'Red', 4, 'black']
Reversed list:  ['black', 4, 'Red', 2, 1]
Last element:  black

Getting the last element of a list by using the pop() function

The pop() function takes the index value as a parameter and removes the value at that particular index. If the parameter is not passed, the pop() function by default removes the last item from the list and returns the value. The main drawback is that it modifies the original list by deleting an item.

Consider a list list_1 = [1, 2, "Red", 4, "black"]. The list_1.pop() returns the item "black" to the variable removed_val and modifies original list to [1, 2, "Red", 4].

list_1 = [1, 2, "Red", 4, "black"]
print("list_1: ", list_1)
removed_val = list_1.pop()
print("Last element: ", removed_val)
print("list_1: ", list_1)

The above code returns the output as

list_1:  [1, 2, 'Red', 4, 'black']
Last element:  black
list_1:  [1, 2, 'Red', 4]
For an empty list, when we try to get the last element using the indexing or pop() function the program raises the IndexError exception.
list1= []
list1.pop()

Output

list1.pop()
IndexError: pop from empty list

Getting the last element of a list by using reversed() + next()

The reversed() function returns a reverse list as an iterator and the next() function returns the next item from an iterator. The next() function takes two arguments iterator, and default value(optional). If the next item is not present in the iterator, the default value is printed.

The syntax for next() function is given below

#syntax:
next(iterator, default(optional))

Consider a list list_1 = [1, 2, "Red", 4, "black"]. The next(reversed(list_1)) gives the first item of list "black" as output.

list_1 = [1, 2, "Red", 4, "black"]
print("list_1: ", list_1)
last_ele = next(reversed(list_1))
print("Last element: ", last_ele)

Output

list_1:  [1, 2, 'Red', 4, 'black']
Last element:  black

If the list is empty, the next() function raises the StopIteration error. To overcome this problem we can pass a default value to the next() function.

list_1 = []
print("list_1: ", list_1)
last_ele = next(reversed(list_1), "No item")
print("Last element: ", last_ele)

Output

list_1:  []
Last element:  No item

Getting the last element of a list by using a for loop

Consider a list list_1 = [1, 2, "Red", 4, "black"]. A for loop iterates over the range of (0, len(list_1)). In each iteration, the program checks if the current index is equal to the last index. If the condition is True, the item at the last index is printed.

list_1 = [1, 2, "Red", 4, "black"]
print("list_1: ", list_1)
for i in range(len(list_1)):
    if i == len(list_1) - 1:
        print("Last element: ", list_1[i])

Output

list_1:  [1, 2, 'Red', 4, 'black']
Last element:  black
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions