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 # 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))
OutputOriginal 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))
OutputOriginal 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))
OutputOriginal 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))
OutputOriginal 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 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 empty Lists from List - Python
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
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 - Append at Front and Remove from Rear
We are given a list we need to append element in front and remove from rear. For example, a = [10, 20, 30] we need to add 5 in front and remove from rear so that resultant output should be [5, 10, 20]. Using insertUsing insert in Python allows us to add an element at the front of a list by specifyin
2 min read
Python - Odd elements removal in List
Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge
5 min read