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:
# 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))
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
# 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))
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
# 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:
# 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:
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4