Skip to content
Related Articles

Related Articles

Python | How to get the last element of list

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 01 Jul, 2022

In this article, we will use 6 ways to get the last element of a list in Python. 

The different methods are listed as:

  • Using Reverse Iterator
  • Using negative indexing
  • Using list.pop()
  • Using reversed() + next() 
  • Using slicing
  • Using itemgetter

List, being an essential python container is used in day-day programming and also in web development. Knowledge of its operations is necessary. Let’s see all the different ways of accessing the last element of a list. 

Method 1: Get the last item of a list through Reverse Iterator

To demonstrate accessing the last element of the list using the naive method in Python. There can be 2-naive methods to get the last element of the list. 

  • Iterating the whole list and getting, the second last element.
  • Reversing the list and printing the first element.

Python3




# initializing list
test_list = [1, 4, 5, 6, 3, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# First naive method
# using loop method to print last element
for i in range(0, len(test_list)):
 
    if i == (len(test_list)-1):
        print("The last element of list using loop : "
              + str(test_list[i]))
 
# Second naive method
# using reverse method to print last element
test_list.reverse()
print("The last element of list using reverse : "
      + str(test_list[0]))

Output :

The original list is : [1, 4, 5, 6, 3, 5]
The last element of list using loop : 5
The last element of list using reverse : 5

Method 2: Get the last item of a list using negative indexing

To demonstrate accessing the last element of the list using the [] operator, the last element can be assessed easily if no. of elements in the list are already known. There is 2 indexing in Python that points to the last element in the list.

  • list[ len – 1 ] : Points to last element by definition.
  • list[-1] : In python, negative indexing starts from the end.

Python3




# initializing list
test_list = [1, 4, 5, 6, 3, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using len - 1 index to print last list element
print("The last element using [ len -1 ] is : "
      + str(test_list[len(test_list) - 1]))
 
# using -1 index to print last list element
print("The last element using [ -1 ] is : "
      + str(test_list[-1]))

Output :

The original list is : [1, 4, 5, 6, 3, 5]
The last element using [ len -1 ] is : 5
The last element using [ -1 ] is : 5

Method 3: Get the last item of a list using list.pop()

To demonstrate accessing the last element of the list using list.pop(), the list.pop() method is used to access the last element of the list. The drawback of this approach is that it also deletes the list’s last element, hence is only encouraged to use when the list is not to be reused. 

Python3




# initializing list
test_list = [1, 4, 5, 6, 3, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using pop() to print last list element
print("The last element using pop() is : "
      + str(test_list.pop()))

Output :

The original list is : [1, 4, 5, 6, 3, 5]
The last element using pop() is : 5

Method 4 : Get the last item of a list using reversed() + next() 

To demonstrate accessing the last element of the list using reversed() + next(), the reversed() coupled with next() can easily be used to get the last element, as, like one of the naive methods, the reversed method returns the reversed ordering of list as an iterator, and next() method prints the next element, in this case, last element. 

Python3




# initializing list
test_list = [1, 4, 5, 6, 3, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using reversed() + next() to print last element
print("The last element using reversed() + next() is : "
      + str(next(reversed(test_list))))

Output :

The original list is : [1, 4, 5, 6, 3, 5]
The last element using reversed() + next() is : 5

Method 5: Get the last item of a list by slicing

In this example, we will use list slicing to get the last element from the list.

Python3




# initializing list
test_list = [1, 4, 5, 6, 3, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
li = last_elem = test_list[-1:][0]
 
# using slicing to print last element
print("The last element using slicing is : ", li)

Output:

The original list is : [1, 4, 5, 6, 3, 5]
The last element using slicing is :  5

Method 6: Get the last item of a list using itemgetter

The itemgetter can be used instead of lambda functions.  In terms of performance over time, itemgetter is better than lambda functions. When accessing many values, it appears more concise than lambda functions. Here, we will use itemgetter to get out the last element in the list.

Python3




import operator
 
# initializing list
test_list = [1, 4, 5, 6, 3, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
li = last_elem = operator.itemgetter(-1)(test_list)
 
# using itemgetter to print last element
print("The last element using itemgetter is : ", li)

Output:

The original list is : [1, 4, 5, 6, 3, 5]
The last element using itemgetter is :  5

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!