Remove last K elements of list - Python
Last Updated :
11 Jul, 2025
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 slicing
List slicing is one of the most efficient and simplest ways to remove the last K elements from a list. With this method, we can easily create a new list that contains all the elements except for the last K elements.
Python
a = [1, 2, 3, 4, 5]
k = 2
res = a[:-k] if k else a
print(res)
Explanation:
- List slicing allows us to select a portion of the list based on indices.
- a[:-k] takes all elements from the start of the list to the K-th last element, effectively removing the last K elements.
- If K is zero, it returns the original list without modification.
Let’s explore some more methods and see how we can remove the last K elements from a list.
Using list.pop()
Another approach is to use the pop() method. This method removes the last element from the list. By calling pop() in a loop K times, we can remove the last K elements from the list.
Python
a = [1, 2, 3, 4, 5]
k = 2
for _ in range(k):
a.pop()
print(a)
Explanation:
- pop() method removes the last element from the list.
- By using a loop and calling pop() K times, we can effectively remove the last K elements.
- This method modifies the list in place repeatedly.
Using del()
del() statement allows us to remove elements from a list by specifying an index range. This method works by deleting elements in place without creating a new list.
Python
a = [1, 2, 3, 4, 5]
k = 2
del a[-k:]
print(a)
Explanation:
- del a[-k:] deletes the last K elements from the list in place.
- It directly modifies the list and doesn't create a new one.
- This method is more efficient than using pop() in a loop but less concise than slicing.
Using list.remove()
remove() method searches for an element to remove, and since we want to remove the last K elements, we would call remove() K times, each time on the last element of the list.
Python
a = [1, 2, 3, 4, 5]
k = 2
for _ in range(k):
a.remove(a[-1])
print(a)
Explanation:
- remove() method removes the first occurrence of the specified element from the list.
- In this case, we use remove(a[-1]) to remove the last element from the list by passing the last element explicitly.
Similar Reads
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 elements at Indices in List - Python In Python, lists store items in a specific order and each item has an index. Removing elements by index means creating a new list that excludes items at certain positions. This helps in keeping only the needed elements while discarding others based on their index. For example:Input : li= [5, 6, 3, 7
2 min read
Remove elements at Indices in List - Python In Python, lists store items in a specific order and each item has an index. Removing elements by index means creating a new list that excludes items at certain positions. This helps in keeping only the needed elements while discarding others based on their index. For example:Input : li= [5, 6, 3, 7
2 min read
Remove elements at Indices in List - Python In Python, lists store items in a specific order and each item has an index. Removing elements by index means creating a new list that excludes items at certain positions. This helps in keeping only the needed elements while discarding others based on their index. For example:Input : li= [5, 6, 3, 7
2 min read
Python - Modulo K elements removal 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
6 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