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
# Python 3 code to demonstrate # Remove rear element # using pop(-1) # initializing list test_list = [ 1 , 4 , 3 , 6 , 7 ] # Printing original list print ( "Original list is : " + str (test_list)) # using pop(-1) to # Remove rear element test_list.pop( - 1 ) # Printing modified list print ( "Modified list is : " + str (test_list)) |
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
# Python 3 code to demonstrate # Remove rear element # using del list[-1] # initializing list test_list = [ 1 , 4 , 3 , 6 , 7 ] # Printing original list print (& quot Original list is : & quot + str (test_list)) # using del list[-1] to # Remove rear element del test_list[ - 1 ] # Printing modified list print (& quot Modified list is : & quot + str (test_list)) |
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
# Python 3 code to demonstrate # Remove rear element # initializing list test_list = [ 1 , 4 , 3 , 6 , 7 ] # Printing original list print ( "Original list is : " + str (test_list)) # Remove rear element test_list = test_list[: len (test_list) - 1 ] # Printing modified list print ( "Modified list is : " + str (test_list)) |
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
# Python 3 code to demonstrate # Remove rear element # initializing list test_list = [ 1 , 4 , 3 , 6 , 7 ] # Printing original list print ( "Original list is : " + str (test_list)) # Remove rear element using list comprehension test_list = [x for x in test_list if test_list.index(x) ! = len (test_list) - 1 ] # Printing modified list print ( "Modified list is : " + str (test_list)) |
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
# Python 3 code to demonstrate # Remove rear element # using list.remove() # initializing list test_list = [ 1 , 4 , 3 , 6 , 7 ] # Printing original list print ( "Original list is : " + str (test_list)) # using list.remove() to # Remove rear element test_list.remove(test_list[ - 1 ]) # Printing modified list print ( "Modified list is : " + str (test_list)) |
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
# Python 3 code to demonstrate # Remove rear element # using pop() with index # initializing list test_list = [ 1 , 4 , 3 , 6 , 7 ] # Printing original list print ( "Original list is : " + str (test_list)) # Using pop() with index to remove last element test_list.pop( len (test_list) - 1 ) # Printing modified list print ( "Modified list is : " + str (test_list)) |
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.