Python – Remove rear element from list
Stack data structure is very well known data structure, lists in Python usually appends 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 i’th element from the list. This method is mostly used among the other available options to perform this task. This changes the original list.
# Python 3 code to demonstrate # Remove rear element# using pop(-1) # initializing list test_list = [1, 4, 3, 6, 7] # Printing original listprint ("Original list is : " + str(test_list)) # using pop(-1) to# Remove rear elementtest_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]
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.
# Python 3 code to demonstrate # Remove rear element# using del list[-1] # initializing list test_list = [1, 4, 3, 6, 7] # Printing original listprint ("Original list is : " + str(test_list)) # using del list[-1] to# Remove rear elementdel 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]


