Given a list of numbers or strings, the task is to write a Python program to remove the last element from a list.
Example:
Input : [12,53,11,76,22,21] Output : [12,53,11,76,22] Input : ["abc","gfg","text","python"] Output : ["abc","gfg","text"]
The application of this type of problem in day-day programming when sometimes requires getting all the elements of the list except the last one. Let’s discuss certain ways in which this task can be performed.
Using len() function and slicing method
Here, we will use the len() function to find out the length of the input_list and then remove storing the whole list except the last element in the new list, i.e. res.
Then print the res list.
Python3
# Input List input_list = [ 1 , 4 , 6 , 3 , 5 , 8 ] print ( "The original list is : " + str (input_list)) # Using len() + list slicing # Remove last element res = input_list[: len (input_list) - 1 ] # Printing result print ( "The list after removing last element : " + str (res)) |
Output :
The original list is : [1, 4, 6, 3, 5, 8]
The list after removing last element : [1, 4, 6, 3, 5]
In Method 1, The new list created can increase the space complexity, to optimize we can use pop() function.
The time complexity is O(n), where n is the length of the input_list.
The auxiliary space is also O(n)
Using pop() function to Remove the last element from list
In this method, the pop() function is used to delete the last element. The same list is printed, which will eventually not print the last element.
Python3
# Input List input_list = [ 'abc' , 'gfg' , 'text' , 'python' ] print ( "The original list is : " + str (input_list)) # Removing last element input_list.pop() # Printing result print ( "The list after removing last element : " + str (input_list)) |
Output :
The original list is : ['abc', 'gfg', 'text', 'python']
The list after removing last element : ['abc', 'gfg', 'text']
Pop() function is better but the problem with that is it removes the elements permanent and we can never get it back.
So, for this python has a special feature of slicing where we can directly prints the list except last element. And whenever we need the full list we can print it as well.
Using slicing with a negative index value
Syntax for slice function : arr [start : stop]. So for the last element use -1.
Python3
# Input List input_list = [ 13 , 34 , 36 , 23 , 15 , 88 ] print ( "The original list is : " + str (input_list)) # printing result using negative index value print ("The list after removing last element : \ " + str (input_list[: - 1 ])) |
Output :
The original list is : [13, 34, 36, 23, 15, 88]
The list after removing last element : [13, 34, 36, 23, 15]
Using del keyword method to Remove the last element from list
We can use the del keyword to remove the last element of the list
Algorithm
1. Initialize a list with elements
2.Use the del keyword with negative index
3. Print the updated list
Code
Python3
# Input List input_list = [ 13 , 34 , 36 , 23 , 15 , 88 ] print ( "The original list is : " + str (input_list)) # Using del keyword to remove the last element del input_list[ - 1 ] # Delete the last element of the list using negative index -1 # printing result using Del keyword print ("The list after removing last element : \ " + str (input_list)) |
The original list is : [13, 34, 36, 23, 15, 88] The list after removing last element : [13, 34, 36, 23, 15]
Next Article – https://www.geeksforgeeks.org/remove-last-element-from-list-in-python/