Skip to content
Related Articles

Related Articles

Python | Remove last K elements of list

View Discussion
Improve Article
Save Article
  • Last Updated : 14 Sep, 2022
View Discussion
Improve Article
Save Article

We often come to situations in which we need to decrease the size of the list by truncating the k last elements of the list. This has its application in the day-day programming when sometimes we require getting all the lists of similar size or removing the last few records from a list. Let’s discuss a few ways in which this task can be performed.

Using len() + list slicing to remove last K elements of list

List slicing can perform this particular task in which we just slice the first len(list) – K elements to be in the list and hence remove the last K elements.

Here we find the length of the current list and calculate the length that will be after removing the n elements (len_l – l), then using slicing, we create a copy list, where it has last n elements removed.

Python3




# initializing list 
test_list = [1, 4, 6, 3, 5, 8]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using len() + list slicing
# remove last K elements
res = test_list[: len(test_list) - K]
 
# printing result
print ("The list after removing last K elements : " +  str(res))

Output

The original list is : [1, 4, 6, 3, 5, 8]
The list after removing last K elements : [1, 4, 6]

Using Negative list slicing to remove last K elements of list

We can perform this particular task using the negative list slicing, in which we start removing the elements from the last index of the list and hence remove all the K elements from the last. If the negative index value is more than the length of the list, it will return an empty list.

Python3




# initializing list
test_list = [1, 4, 6, 3, 5, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using negative list slicing
# remove last K elements
res = test_list[:-K]
 
# printing result
print("The list after removing last K elements : " + str(res))

Output:

The original list is : [1, 4, 6, 3, 5, 8]
The list after removing last K elements : [1, 4, 6]

Using Python List pop() Method to remove last K elements of list

The pop() method will remove the last element from the list, So to remove the last k elements from the Python List, we need to perform the pop() operation k times.

Python3




# initializing list
test_list = [1, 7, 6, 3, 5, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using pop()
# remove last K elements
for i in range(K):
    test_list.pop()
 
# we can also do list comprehension to pop()
# _ = [test_list.pop() for _ in range(K)]
 
# printing result
print("The list after removing last K elements : " + str(test_list))

Output:

The original list is : [1, 7, 6, 3, 5, 8]
The list after removing last K elements : [1, 7, 6]

Using del keyword and list slicing to remove last K elements of list

Here we return a sliced range and delete it from the list using Python del keyword.

Python3




# initializing list
test_list = [1, 7, 6, 3, 5, 8]
# remove last 3 elements
K = 3
# using del on negative slicing
del test_list[-3:]
 
print("List after removing elements:", test_list)

Output:

List after removing elements: [1, 7, 6]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!