Get last item of a list using negative indexingList 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, Show
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 PythonIndexing 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. 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: 7Alternatively, 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: 7We 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,
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 PythonMethod 1 – By Iterating all the elements in a listThe 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 : 6Method 2 – Using the reverse() methodThe 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 : 6Method 3 – Using pop() methodThe 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 : 6Method 4 – Using Negative Indexing [] OperatorThe 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 : 6Method 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’show 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 answers related to “how to get the last element in a list python”
Python queries related to “how to get the last element in a list python”
Python | How to get the last element of listList, 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.
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
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.
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
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.
Output : The original list is : [1, 4, 5, 6, 3, 5] The last element using pop() is : 5
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.
Output : The original list is : [1, 4, 5, 6, 3, 5] The last element using reversed() + next() is : 5Attention 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 PythonTo get the last element of the list in Python, we have two ways.
Let’s see these approaches one by one. Python list last elementTo 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)Outputastral |