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


In this article, we will show you how to get the last element of the input list using python. Now we see 4 methods to accomplish this task −

  • Using negative indexing

  • Using slicing

  • Using pop() method

  • Using For loop

Assume we have taken a list containing some elements. We will return the last element of the given input list using different methods as specified above.

Method 1: Using negative indexing

Python allows for "indexing from the end," i.e., negative indexing.

This means that the last value in a sequence has an index of −1, the second last has an index of −2, and so on.

When you want to pick values from the end (right side) of an iterable, you can utilize negative indexing to your benefit

Syntax

list[len − 1]: By definition, points to the last element.
list[−1]: Negative indexing starting from the end

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list

  • Get the last element of the list using the “length of list - 1” as an index and print the resultant last element of the list.

  • Get the last element of the list using − 1(negative indexing) as the index and print the resultant last element of the list.

Example

The following program returns the last element of the input list using negative indexing −

# input list inputList = [5, 1, 6, 8, 3] # printing input list print("Input list:", inputList) # getting the last element of the list using the length of the list - 1 as an index(Here list index starts from 0 so list length -1 gives the index of the last element of the list) print("Last element of the input list using len(list)-1 as index = ", inputList[len(inputList) - 1]) # getting the last element of the list using - 1 as the index print("Last element of the input list using -1 as index = ", inputList[-1])

Output

On executing, the above program will generate the following output −

Input list: [5, 1, 6, 8, 3]
Last element of the input list using len(list)-1 as index = 3
Last element of the input list using -1 as index = 3  

Method 2: Using slicing

List slicing is a frequent practice in Python, and it is the most commonly utilized way for programmers to solve efficient problems. Consider a Python list. To access a range of elements in a list, you must slice it. One method is to utilize the simple slicing operator, i.e. colon (:)

With this operator, one can define where to begin slicing, where to end slicing and the step. List slicing creates a new list from an old one.

Syntax

List[ start : end : step]

Parameters

start- index from where to start

end - ending index

step - numbers of jumps to take in between i.e stepsize

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Get the last element of the input list using slicing and create a variable to store it.

inputList[-1:][0] represents getting the first element by slicing it from the end of the list   
  • Print the last element of the input list.

Example

The following program returns the last element of the input list using slicing −

# input list inputList = [5, 1, 6, 8, 3] # printing input list print("Input list:", inputList) # getting the last element of the list using slicing lastElement = inputList[-1:][0] # printing the last element of the list print("Last element of the input list = ", lastElement)

Output

Input list: [5, 1, 6, 8, 3]
Last element of the input list = 3

Method 3: Using pop() method

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Give the input list

  • Using the pop() function (removes the last element from a list and returns it), for getting the last element of the list

  • print the resultant last element of the list.

Example

The following program returns the last element of the input list using the pop() function −

# input list inputList = [5, 1, 6, 8, 3] # printing input list print("Input list:", inputList) # getting the last element of the list using pop() method print("Last element of the input list = ", inputList.pop())

Output

Input list: [5, 1, 6, 8, 3]
Last element of the input list = 3

Method 4: Using For loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the for loop, to traverse till the length of the list using the len() function (The number of items in an object is returned by the len() method)

  • Use the if conditional statement, to check whether the iterator value is equal to the length of the list -1.

  • Print the corresponding element of the list, if the condition is true.

Example

The following program returns the last element of the input list using the for loop −

# input list inputList = [5, 1, 6, 8, 3] # printing input list print("Input list:", inputList) # Traversing till the length of the list for k in range(0, len(inputList)): # Checking whether the iterator value is equal to the length of list -1(last element) # Here == is used to check whether the iterator value is equal to list length-1 if k == (len(inputList)-1): # Printing the corresponding element of the list, # if the condition is true print("Last element of the input list = ", inputList[k])

Output

Input list: [5, 1, 6, 8, 3]
Last element of the input list = 3

Conclusion

We learned how to get the last element of a list using four different methods in this article: negative indexing, pop() function, negative indexing, and looping. We also learned how to use the pop() function to remove the last element from a list.

Updated on: 23-Aug-2023

78K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements