The index of the last item in a list is the length of the list.

Get last item of a list using negative indexing

List in python supports negative indexing. So, if we have a list of size “S”, then to access the Nth element from last we can use the index “-N”. Let’s understand by an example,
Suppose we have a list of size 10,

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
To access the last element i.e. element at index 9 we can use the index -1,
# Get last element by accessing element at index -1 last_elem = sample_list[-1] print('Last Element: ', last_elem)
Output:
Last Element: 9
Similarly, to access the second last element i.e. at index 8 we can use the index -2.
last_elem = sample_list[-2]
Output:
Last Element: 8
Using negative indexing, you can select elements from the end of list, it is a very efficient solution even if you list is of very large size. Also, this is the most simplest and most used solution to get the last element of list. Let’s discuss some other ways,

Get the last element of a list using indexing in Python

Indexing in python is a way to access elements from a list. In python, we can use positive indices as well as negative indices. Positive indices start with zero which corresponds to the first element of the list and the last element of the list is identified by the index “listLen-1” where “listLen” is the length of the list.

Alternatively, negative indices start from -1 which corresponds to the last element of the list. It goes till “-listLen” where listLen is the length of the list. The index “-listLen” corresponds to the first element in the list.

The index of the last item in a list is the length of the list.

To get the last element of a list, we can first find the length of the list using the len() function. Then we can access the last element of the list at the index “listLen-1” as follows.

myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) listLen = len(myList) lastElement = myList[listLen - 1] print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

Alternatively, we can use negative indexing to access the last element of the list. The last element of the list is at index -1 which can be accessed as follows.

myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) lastElement = myList[- 1] print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

We can see that while using negative indexing, we don’t have to calculate the length of the list.

In Python, how do you get the last element of a list?

To just get the last element,

  • without modifying the list, and
  • assuming you know the list has a last element (i.e. it is nonempty)

pass -1 to the subscript notation:

>>> a_list = ['zero', 'one', 'two', 'three'] >>> a_list[-1] 'three'

What is a List in Python?

The list is one of the most commonly used data types in Python. A list is a collection of elements that can be of any data type. A single list can contain a combination of numbers, strings, nested lists, etc.

How to Get the Last Element of a List in Python

Method 1 – By Iterating all the elements in a list

The most common and straightforward way to get last element in list is through iterating the entire element till it reaches the last element, as shown below.

# Python code to access the last element # in a list using for loop # Declaring and initializing list number_list = [2, 1, 7, 4, 5, 3,6] print ("List of elements are : " + str(number_list)) # using loop method to print last element for i in range(0, len(number_list)): if i == (len(number_list)-1): print ("The last element of list is : "+ str(number_list[i])) # Output List of elements are : [2, 1, 7, 4, 5, 3, 6] The last element of list is : 6

Method 2 – Using the reverse() method

The reverse()method in Python is used to reverse the elements of a list. The reverse() method does not take any arguments and does not return any values instead, it reverses the existing list.

# Python code to access the last element # in a list using reverse() method # Declaring and initializing list number_list = [2, 1, 7, 4, 5, 3,6] print ("List of elements are : " + str(number_list)) # using reverse method to print last element number_list.reverse() print("The last element of list using reverse method are : " + str(number_list[0])) # Output List of elements are : [2, 1, 7, 4, 5, 3, 6] The last element of list using reverse method are : 6

Method 3 – Using pop() method

The pop() method is used to access the last element of the list and returns the removed item. Thepop() method accepts an integer as the argument, which is basically the index of the item which needs to be removed from the list.

Example – list.pop(0) will remove and return the first element in the list.

Note – Calling the pop()method will remove the last item from the list itself. Hence if you are re-using the list, this approach is not recommended as the element is removed.

# Python code to access the last element # in a list using pop() method # Declaring and initializing list number_list = [2, 1, 7, 4, 5, 3,6] print ("List of elements are : " + str(number_list)) # using pop() method to print last element print("The last element of list using reverse method are : " + str(number_list.pop())) # Output List of elements are : [2, 1, 7, 4, 5, 3, 6] The last element of list using reverse method are : 6

Method 4 – Using Negative Indexing [] Operator

The negative index is the way of indexing the element from the end of the list with an index starting from -1, i.e., -1 gives the last element, -2 gives the last 2nd element, and so on.

The major use of negative indexing can be used to reverse a list without using any built-in function.

You can also find the last element in the list using length-1. In order to access the last element, we need to know the length of the list.

# Python code to access the last element # in a list using negative indexing # Declaring and initializing list number_list = [2, 1, 7, 4, 5, 3,6] print ("List of elements are : " + str(number_list)) # using length-1 to print last element print("The last element of list using length-1 method are : " + str(number_list[len(number_list) -1])) # using [] operator to print last element print("The last element of list using reverse method are : " + str(number_list[-1])) # Output List of elements are : [2, 1, 7, 4, 5, 3, 6] The last element of list using length-1 method are : 6 The last element of list using reverse method are : 6

Method 5 – Using itemgetter()

The operator module in Python has vast features to perform all mathematical, logical, comparison operations.

Theitemgetter()method is one of the methods in the operator module, and it accepts one or more integer arguments and returns the callable object.

import operator # Python code to access the last element # in a list using itemgetter() method # Declaring and initializing list number_list = [2, 1, 7, 4, 5, 3,6] print ("List of elements are : " + str(number_list)) getLastElement = operator.itemgetter(-1) # using [] operator to print last element print("The last element of list using reverse method are : " + str(getLastElement(number_list))) # Output List of elements are : [2, 1, 7, 4, 5, 3, 6] The last element of list using reverse method are : 6

“how to get the last element in a list python” Code Answer’s


how to get the last value in a list python
python by Fancy Fly on Jun 22 2020 Comment
2
how to find the last item of a list
python by Inexpensive Ibis on Jul 06 2020 Comment
4
find last element in list python
python by on Jun 07 2021 Comment
0
Source: stackoverflow.com
Add a Grepper Answer

  • python how to get the last element in a list
  • last element of list python
  • last element in list py
  • how to print last element in a list python
  • get every item but the last item of python list
  • how to get last element of list in python
  • python last element of list
  • python list last element
  • python last item in list
  • get the last element from the list
  • python get the last element from the list

  • get last element of list python
  • how to get the last element of a list in python
  • python get last element in list
  • get last item in list python
  • py how to get the last item in a list
  • python last element in array
  • python return last item in list
  • how to print the last thing in a list python
  • how to get last element in list java
  • get last element python list
  • how to find the last element of list in python
  • last element of array python
  • get last element in array python
  • getting '' in end of list
  • python array last one
  • how to get last element of a list in python
  • read last element of list python
  • python get last element of set
  • check if element is last in list python
  • get last cell in list python
  • get the last item in a list r
  • get last index of list python
  • python last item in list
  • last in list python
  • get last element in list java
  • last element list python
  • print last elements of list python
  • get last n elements of list python
  • python list get everything but last item
  • java how to get last element of list
  • get last 10 values in list
  • get last value of list python
  • how to get the last item of a list in python
  • get last 5 elements of list python
  • find last element of list python
  • get last element of list and then the rest
  • how to get the last value in list
  • identify the last element in list java
  • array list get last item
  • java access last element in the list
  • display last element of list python
  • find last element of character in list python
  • integer list to get last element
  • last element array python
  • last element of list in java
  • how to check last item in list python
  • last value in list java
  • how to find last 3rd last element in list pizule
  • python list get last element
  • print the last five elements of a list python
  • how to print last item on fhe array list
  • the last item in a list is labeled an c++
  • python last 5 elements of list
  • get last eleemtn of list python
  • check if is the last element of a list in django
  • how to get last item in python array
  • how to get last element in a list python
  • list get all but last item array python
  • python get last element list
  • get last item python
  • python last index of element in list
  • python get index of last list element
  • print last element of a list in python
  • find last items in list python
  • python get last n items in list
  • find last python
  • how to get the last object in a list
  • take the last element of a list python
  • last element of a list python
  • printing last element of list in python
  • get last element in java list
  • python get last list
  • get the last vaslue of an array list
  • how to get last item of list python
  • python array get last element
  • index last element in list [ython
  • get the last element from list java
  • how to get the last item in list loop
  • list last item index python
  • get last of array python
  • get last n elements in list python
  • how to search elements in list from last
  • flatlist how to know last of the list
  • get last occurrence in list
  • python how to get last value of array
  • java get last element in list
  • end of array in python
  • python last 10 elements of list
  • get last val of array pythob
  • get last index of element in list python
  • python get last item from list
  • how to find the last value in a list in python
  • check the last element of list python
  • how to get last element of list inpython
  • python how to do something to the last object of a list
  • how to get last element in array python
  • get last 3 elements of list python
  • list get last element
  • python get the last maximum value in list
  • list get last element python
  • python list last item
  • to give last element of a list python
  • python last 10 values in list
  • choose last element of list python
  • how to get the index of the last number in a list python
  • alst elment of list python
  • how to print the last item in a list python
  • python find last el in list
  • how to check last element in list python
  • last element of list if list has only one element
  • python list take last element
  • get last dict of a list
  • how get last index in array placing in python
  • getting the last element of a list in python
  • python get last element in a list
  • how to grab the latest thng in a list in python
  • get last array in python
  • python get last position in list
  • python print last value in list
  • key function to extract the last element python
  • how to get last element of
  • how to excess last element from list in python using index
  • get last object python
  • how to select the last element in a list python
  • python get last item in array
  • get last element array pythin
  • value last element list python
  • python call last item in list
  • return last element of array python
  • getting the last item in a list pytohn
  • python for get last item in the list
  • python last.get
  • print last number in list python
  • python array end
  • get last element in python array
  • how to get last two value from pyython list
  • python functions only returning last value
  • slcie last element python
  • last term in list python
  • python get last n values from list
  • get last item in tuple python
  • find last postiion of value in python list
  • get last four items in list python
  • get the last three value from a list python
  • python how to get the last value of list
  • last number in list python
  • last element in an array python a[:-1]
  • get last value in list python
  • python list is last
  • python get last occurrence in list
  • what is the last index in a python list
  • python last element in list
  • how to get last element of list in python
  • get last element of array python
  • check if is the last element in list python
  • how to access the last element of a list in python
  • how to get the last value in a list python
  • how to get the last item in a list python
  • python list last element
  • python get last items in list
  • list last item
  • access the last element in a list python
  • how to check last element of list in python
  • get last item in array python
  • position last elements list python
  • python last n elements of list
  • how to get last item in list python
  • get last item of list
  • find last item in array python
  • select last item in array python
  • python list last n elements
  • python print last element in list
  • how to print last element in a list python
  • python select last element in list
  • how to get last index of list in python
  • last element in list python
  • python check if element is last in list
  • get last array python
  • last element of an array python
  • how to get last element of array python
  • get last postition of list python
  • last item in list
  • print the last item in a list python
  • list.last python
  • last element of a list java
  • python get last element of a list
  • python query to get the last element in a list
  • list get last element java
  • get last three elements of list python
  • python how to get last index of list
  • pyhton list.last
  • how to get last value in list python
  • index of last element of list
  • how to find last element value from list in java
  • get the last element of a list python
  • find last index of element in list python
  • java last element in list
  • get the last n elements of a list python
  • get last items in array python
  • how to access last element of list in java
  • last element of list java
  • python get the last argument from a list
  • get last element of the list
  • python find last
  • last on a list in python
  • python check if element is last element in list
  • array last value python
  • how to get the last element of an array python
  • list last element python
  • get the last item in a list python
  • python last element of list
  • how to find the last index of a list in python
  • how to find last entry of array python
  • python3 find last occurrence in list
  • python last element list
  • return last element of list python
  • get last element from array python
  • last item in a list pzthon
  • get last element of the list py
  • hoe to get last index if list in python
  • returns the data-type of the last element in a list
  • python list last items
  • how to find the last element of list a[]
  • python last item array
  • last value in a list java
  • python list last element -1
  • get last 10 items in list
  • get last element of a list python
  • print last element in array python
  • print last 100 element of list python
  • check if last item in list python
  • getting last element of list python
  • last item in the list in java
  • how to get the last element of an array in python
  • get last element of a list
  • python list indexing last element
  • how to find last element of list in python
  • determinne last element of list in array
  • python take off last item in list
  • python list get last entry
  • how to get last elemnt of an array in python
  • how to get last value in list
  • python most efficient way to get last element of list
  • finding last element of an array in python
  • python get last in array
  • python entire list but last element
  • how to see the last item added to a set python
  • last item list python
  • get last item of list python
  • how to get the last 3 elements of a list in python
  • the very last on the list
  • get last element of an list
  • python print last item in list
  • python regular expression get last element of list
  • llast element in array in python
  • how to find last element in array python
  • get to end of list python
  • how to find the last object in a python list
  • how to get the last item in an array python
  • how to print the last item in a list in python
  • last element of a list
  • get last arry valye in python
  • last value array python
  • find last item of list python
  • how to index last element in list python
  • getting the last element of a list in python as a list
  • python get last to items in array
  • python select last item on list
  • last element of an array in python
  • python print array end
  • how to read last of list
  • how to find last element in list python
  • print the last item in the list using negative indexing.
  • last elem list python
  • python select last item in object
  • python acces last element of array
  • how to select last item in list python
  • last element of a pythons arrat
  • take last value of array list python
  • how to print and at the last of the list item
  • python get the last item in list
  • how to check the last place of a list in python
  • is last element in list
  • get latest element in list
  • how to run enumerate from last of array in python
  • find last in python\
  • get the last element of array python
  • get last index in list python
  • find last date from a list python
  • [0:last] in list
  • how to call the last item from a list python
  • last item python
  • get the last element of a list python method
  • python list end-1
  • python 3 last element in array
  • check last value of list python
  • python get last value in array
  • python array last element
  • last element of list
  • how to represent the last position in a list python
  • get last element of list using for loop python
  • last element of list python
  • python get last element of list
  • get last element in list python
  • access last element of list python
  • python get last element of array
  • python get last item in list
  • how to get last element of array in python
  • java get last element of list
  • check last element in list python
  • how to get the last element in a list python
  • select last element of list python
  • accessing last element in list python
  • find last occurrence of element in list python
  • access last element of array python
  • how to call last threee items of list in python
  • how to find the last index in a integer list
  • how to print last value of list in python
  • how to print the last element of a list in python
  • how to get last n elements of a list in python
  • how to find the last item of a list
  • how to get the last number in a list python
  • python last element
  • get latest element in array python
  • how to target the last sequence on the list
  • check if item is last in list python
  • get last element of list java
  • python get last n elements of list
  • get last item in an array python
  • list get all but last item array
  • how to get last element of a list in java
  • last element in list lisp
  • java how to get the last element of a list
  • last item array python
  • python get last list elements
  • list last value list
  • retrieving last element in python list is o(1)?
  • scheme get last element in list
  • list get last 5 items
  • index of last item in list python
  • last element of the list pyhton
  • access last element of a numbered list python
  • print last value of list python
  • get last value on list<object>
  • hoe to get the last item in a list python
  • last element in list java
  • get last item list python
  • get list last index
  • python get the last element of a list
  • how to get the last item of a python list
  • get last item in list
  • list last element
  • get last item in a list python
  • get last in array python
  • get list but last value python
  • python access last element of list
  • python select last item in list
  • python take last element of list
  • write function called last element in list in python
  • how to find last index of an element in a list python
  • find last element in list prolog
  • acces last element array pyhton
  • python 3 last elements of a list
  • list find last index of element
  • python array get last element
  • python get last element value
  • get last occurence in list
  • acces the last number of a list
  • how to find last element of list python
  • python last index of element in list for
  • get last item from list view
  • how to read last element from array in python
  • how to get last item of list java
  • how to find the last element of a nested list in python
  • get last value from list python
  • python how to look at the last item in a list
  • position of last number in a list python
  • how to get the last value of a list in python
  • java find last element in list
  • get the last element of a series python
  • python get the last element of objects in a list
  • get last 10 elements of list python
  • get last in list python
  • how to print first and last word in list in python
  • select only the last in list
  • python last element array
  • python print last argument in list
  • get last 5 values in list python
  • get last element of list in python
  • access last element form the list in python
  • python get the last item in a list
  • hwo to get the last element of an array python
  • print last element of list python
  • how to find last number in a listt in in python
  • list last index in python
  • python access last 20 items in list
  • python last index of item in list
  • get the last value in a list of tuples python
  • how to find last index of list in python
  • get the last element in an array python
  • how to get last 3 elements of list in python
  • last item in array python
  • last element added in list python
  • last lists element python
  • how to find the last element of an array python
  • python get last index of item in array
  • reutn last item in a list python
  • python get list last element
  • shorthand to get the last value in a list python
  • get last element python
  • hwo can u print . for the last one in a list python
  • pythn get last in list
  • latest in list python
  • how to get the last view values of a list in python
  • how to get last element of the list in python
  • fetch last element from list python
  • get last elemet
  • pythn last element of array
  • python print last string of a list
  • python print last item in array
  • python how to pick the last element in a range
  • python define last element
  • why does indexing to array consequetavly return the last one only in python
  • how to view last item in list python
  • js get last element from list that in view
  • return last element of list in python
  • python list index last element
  • python send only the last item in array
  • how to access last item of list python
  • get last python
  • last items from list
  • python3 grab last on the enumerated list
  • print python last list
  • python last elemnt in list
  • getting the last string of an object from a list of objects
  • get the last 5 elements of a list
  • how to get last number in array python
  • print last element of array python
  • python get last return
  • python get last element of a list?
  • python last in array
  • find last in python
  • the last item in a list is labeled an.
  • list python last 4
  • find last element of an array python
  • how to get only last item in list python
  • get the last element in an array in python
  • give the last datetime in python in list
  • python get last value in list
  • python find last of
  • last 10 elements of list python

Python | How to get the last element of list

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 : Naive Method

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.




# Python 3 code to demonstrate
# accessing last element of list
# using naive method
# 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 : Using [] operator

The last element can be assessed easily if no. of elements in list are already known. There are 2 index in Python that point to last element in list.

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




# Python3 code to demonstrate
# accessing last element of list
# using [] operator
# 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 : 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 last element, hence is only encouraged to use when list is not to be reused.




# Python3 code to demonstrate
# accessing last element of list
# using list.pop()
# 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 : Using reversed() + next()

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




# Python3 code to demonstrate
# accessing last element of list
# using reversed() + next()
# 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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python list-programs
python-list
Practice Tags :
python-list

How to Get Last Element of a List in Python

To get the last element of the list in Python, we have two ways.

  1. Using list[-1] syntax and I am assuming that you have a non-empty list.
  2. Using list.pop() method.

Let’s see these approaches one by one.

Python list last element

To get the last element of the list in Python, use the list[-1] syntax. 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 = ["infy", "tcs", "affle", "dixon", "astral"]

In this list, the “astral” 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.

data = ["infy", "tcs", "affle", "dixon", "astral"] last_element = data[-1] print(last_element)

Output

astral