Python – Remove rear element from list
Last Updated :
06 Apr, 2023
A stack data structure is a very well-known data structure, lists in Python usually append the elements to the end of the list. For implementing a stack data structure, it is essential to be able to remove the end element from a list. Let’s discuss the ways to achieve this so that stack data structure can be implemented easily using lists.
Method #1: Using pop(-1)
This method pops, i.e removes and prints the ith element from the list. This method is mostly used among the other available options to perform this task. This changes the original list.
Approach:
- Initialize a list named test_list with some elements.
- Print the original list using the print() function and the str() function to convert the list to a string.
- Use the pop(-1) method to remove the last element from the list. The pop() method modifies the list in-place and returns the removed element.
- Print the modified list using the print() function and the str() function to convert the list to a string.
Python3
test_list = [ 1 , 4 , 3 , 6 , 7 ]
print ( "Original list is : " + str (test_list))
test_list.pop( - 1 )
print ( "Modified list is : " + str (test_list))
|
Output :
Original list is : [1, 4, 3, 6, 7]
Modified list is : [1, 4, 3, 6]
Time complexity: O(1) – The pop() method takes constant time to remove the last element from the list.
Auxiliary space: O(1) – No extra space is used in this code.
Method #2: Using del list[-1] This is just the alternate method to perform the rear deletion, this method also performs the removal of list element in place and decreases the size of list by 1.
Python3
test_list = [ 1 , 4 , 3 , 6 , 7 ]
print (& quot
Original list is : & quot
+ str (test_list))
del test_list[ - 1 ]
print (& quot
Modified list is : & quot
+ str (test_list))
|
Output :
Original list is : [1, 4, 3, 6, 7]
Modified list is : [1, 4, 3, 6]
Time complexity: O(1)
Auxiliary space: O(1)
Method #3 : Using slicing + len() method
Python3
test_list = [ 1 , 4 , 3 , 6 , 7 ]
print ( "Original list is : " + str (test_list))
test_list = test_list[: len (test_list) - 1 ]
print ( "Modified list is : " + str (test_list))
|
Output
Original list is : [1, 4, 3, 6, 7]
Modified list is : [1, 4, 3, 6]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using a list comprehension method.
Using a list comprehension to create a new list without the last element.
Python3
test_list = [ 1 , 4 , 3 , 6 , 7 ]
print ( "Original list is : " + str (test_list))
test_list = [x for x in test_list if test_list.index(x) ! = len (test_list) - 1 ]
print ( "Modified list is : " + str (test_list))
|
Output
Original list is : [1, 4, 3, 6, 7]
Modified list is : [1, 4, 3, 6]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using a list comprehension method.
This approach involves using the built-in method list.remove() to remove the last element from the list by specifying the element to be removed. It does not require the use of additional data structures or creation of a new list.
Approach:
- Create a list test_list with the values [1, 4, 3, 6, 7].
- Print the original list using print(“Original list is : ” + str(test_list)). This will output “Original list is : [1, 4, 3, 6, 7]”.
- Use the remove() method to remove the last element of the list. This is done by calling test_list.remove(test_list[-1]). test_list[-1] returns the last element of the list, which is 7. Then, remove() is called on the list to remove this element.
- Print the modified list using print(“Modified list is : ” + str(test_list)). This will output “Modified list is : [1, 4, 3, 6]”.
Python3
test_list = [ 1 , 4 , 3 , 6 , 7 ]
print ( "Original list is : " + str (test_list))
test_list.remove(test_list[ - 1 ])
print ( "Modified list is : " + str (test_list))
|
Output
Original list is : [1, 4, 3, 6, 7]
Modified list is : [1, 4, 3, 6]
Time Complexity: O(n), The list.remove() method has a time complexity of O(n) in the worst case, where n is the number of elements in the list. This is because it needs to search through the list to find the element to remove.
Auxiliary Space: O(1) This approach does not use any additional auxiliary space since it modifies the original list in place.
Method 5: Using pop() with index
Step-by-step approach:
- Initialize the list
- Get the index of the last element using the len() method and subtracting 1
- Remove the last element using the pop() method with the index as the argument
- Print the modified list
Below is the implementation of the above approach:
Python3
test_list = [ 1 , 4 , 3 , 6 , 7 ]
print ( "Original list is : " + str (test_list))
test_list.pop( len (test_list) - 1 )
print ( "Modified list is : " + str (test_list))
|
Output
Original list is : [1, 4, 3, 6, 7]
Modified list is : [1, 4, 3, 6]
Time complexity: O(1) for the pop() method and O(n) for the len() method, but as n is constant, the time complexity can be considered as O(1).
Auxiliary space: O(1) as we are only removing the last element and not creating any new list or variable.
Similar Reads
Python | Remove random element from list
Sometimes, while working with Python lists, we can have a problem or part of it, in which we desire to convert a list after deletion of some random element. This can have it's application in gaming domain or personal projects. Let's discuss certain way in which this task can be done. Method : Using
3 min read
Python | Remove given element from the list
Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
Python | Remove Front K elements
We often come to situations in which we need to decrease the size of the list by truncating the first elements of the list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar si
7 min read
Remove Multiple Elements from List in Python
In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list. [GFGTABS] Python a = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remov
3 min read
Python - Remove empty List from List
In this article, we will explore various method to remove empty lists from a list. The simplest method is by using a for loop. Using for loopIn this method, Iterate through the list and check each item if it is empty or not. If the list is not empty then add it to the result list. [GFGTABS] Python a
2 min read
Python | Remove given element from list of lists
The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
6 min read
Remove common elements from two list in Python
When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. [GFGTABS] Python a = [1, 2, 3, 4, 5
3 min read
Remove an Element from a List by Index in Python
We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2
3 min read
Remove last K elements of list - Python
Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Letâs explore different methods to remove the last K elements from a list in Python. Using list slicingList slicing is one of
3 min read
Python | Remove particular element from tuple list
Since the advent of the popularity of Python in data analysis, we have a list of tuples as a container in many of our problems. Sometimes, while data preprocessing, we might have a problem in which we need to completely remove a particular element from a list of tuples. Let's discuss a way in which
7 min read