Python | Function that takes a list of integers and returns the last integer

Let us now take a look at the different ways in which we can solve this problem. Here we will be using the concept of negative indexing, string slicing, and many more.

Method 1: Using string slicing to find the last element

def remove_last_element(lst_of_elements):
    li = lst_of_elements[-1:][0]
    print("The last element using slicing is : ", li)


lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
    
print(lst_of_elements)

remove_last_element(lst_of_elements)

Output:

Enter number of elements : 6
1
12
33
45
26
78
[1, 12, 33, 45, 26, 78]
The last element using slicing is :  78

Explanation:

Here we first asked the user about the number of elements that he will be adding to the list. Based on that we ran a for loop till the range of the size of the list. To append each entered digit, we used the append() function of the list. Once that is done, we passed on that lst_of_elements variable to the function named remove_last_element. Now using the concept of string slicing we reversed the string using [-1:] and accessed the element at 0th place

Method 2: Using pop to find the last element

def remove_last_element(lst_of_elements):
    print("The original list is : " + str(lst_of_elements))
    
    print("The last element using pop() is : "
        + str(lst_of_elements.pop()))


lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
    
print(lst_of_elements)

remove_last_element(lst_of_elements)

Explanation:

After accepting the input as explained above, we used the built-in list function named pop(). This basically deletes the last element in the list and we simply print the last element that we popped.

Method 3: Using itemgetter() function to find the last element

import operator

def remove_last_element(lst_of_elements):
    print("The original list is : " + str(lst_of_elements))
    li = last_elem = operator.itemgetter(-1)(lst_of_elements)
    print("The last element using itemgetter is : ", li)

lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
print(lst_of_elements)

remove_last_element(lst_of_elements)

Explanation

The itemgetter(-1) will simply reverse the list and access the first element of it.

Method 4: Using reversed() and next() to find the last element

def remove_last_element(lst_of_elements):
    print("The original list is : " + str(lst_of_elements))
    print("The last element using reversed() + next() is : "
      + str(next(reversed(lst_of_elements))))

lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
print(lst_of_elements)

remove_last_element(lst_of_element)

Explanation

One argument is required by the reversed() method:
a reversible indexable object (sequence_object), which can be a tuple, string, list, range, etc.

returned by reversed()
The reversed() function produces a list of the elements from a sequence object in reverse order.

The next item in an iterator is returned by the next() method.

Method 5: Using reverse() and loop method

def remove_last_element(lst_of_elements):
    print("The original list is : " + str(lst_of_elements))
    
    for i in range(0, len(lst_of_elements)):
        if i == (len(lst_of_elements)-1):
            print("The last element of list using loop : "
                + str(lst_of_elements[i]))
    
    lst_of_elements.reverse()
    print("The last element of list using reverse : "
        + str(lst_of_elements[0]))

lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
print(lst_of_elements)

remove_last_element(lst_of_elements)

Explanation

Talking about the loop method, we used the for loop to first reverse the placement of elements in the list. Once that is done, we simply accessed the first element in the list. Reversing will simply make the last element first and then we can easily access it.

Talking about the reverse() method, we reversed and accessed the element.

Method 6: Using the negative list iteration method

def remove_last_element(lst_of_elements):
   
    print("The original list is : " + str(lst_of_elements))
    
 
    print("The last element using [ len -1 ] is : "
        + str(lst_of_elements[len(lst_of_elements) - 1]))
    
    
    print("The last element using [ -1 ] is : "
        + str(lst_of_elements[-1])) 
    

lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
print(lst_of_elements)

remove_last_element(lst_of_elements)

Explanation

We simply find the length of the list and subtracted 1 from it. That basically becomes the index number of where the last number is located. We passed on that to lst_of_elements and voila, we got our last element.

Share:
Avatar of Dhvanil V

Author: Dhvanil V

Leave a Reply

Your email address will not be published. Required fields are marked *