ItsMyCode |
In this tutorial, let us look at the most efficient ways to find the last element in list in Python.
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. The*pop()
* 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.
The*itemgetter()
*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
The post How to Get the Last Element of a List In Python appeared first on ItsMyCode.
Top comments (0)