How do you remove the last few items in a list python?

As Vincenzooo correctly says, the pythonic lst[:-n] does not work when n==0.

The following works for all n>=0:

lst = lst[:-n or None]

I like this solution because it is kind of readable in English too: "return a slice omitting the last n elements or none (if none needs to be omitted)".

This solution works because of the following:

  • x or y evaluates to x when x is logically true (e.g., when it is not 0, "", False, None, ...) and to y otherwise. So -n or None is -n when n!=0 and None when n==0.
  • When slicing, None is equivalent to omitting the value, so lst[:None] is the same as lst[:] (see here).

As noted by @swK, this solution creates a new list (but immediately discards the old one unless it's referenced elsewhere) rather than editing the original one. This is often not a problem in terms of performance as creating a new list in one go is often faster than removing one element at the time (unless n<<len(lst)). It is also often not a problem in terms of space as usually the members of the list take more space than the list itself (unless it's a list of small objects like bytes or the list has many duplicated entries). Please also note that this solution is not exactly equivalent to the OP's: if the original list is referenced by other variables, this solution will not modify (shorten) the other copies unlike in the OP's code.

A possible solution (in the same style as my original one) that works for n>=0 but: a) does not create a copy of the list; and b) also affects other references to the same list, could be the following:

    lst[-n:n and None] = []

This is definitely not readable and should not be used. Actually, even my original solution requires too much understanding of the language to be quickly read and univocally understood by everyone. I wouldn't use either in any real code and I think the best solution is that by @wonder.mice: a[len(a)-n:] = [].

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

test_list = [1, 4, 6, 3, 5, 8]

print ("The original list is : " + str(test_list))

K = 3

res = test_list[: len(test_list) - K]

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

test_list = [1, 4, 6, 3, 5, 8]

print("The original list is : " + str(test_list))

K = 3

res = test_list[:-K]

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

test_list = [1, 7, 6, 3, 5, 8]

print("The original list is : " + str(test_list))

K = 3

for i in range(K):

    test_list.pop()

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

test_list = [1, 7, 6, 3, 5, 8]

K = 3

del test_list[-3:]

print("List after removing elements:", test_list)

Output:

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

How do I remove the last 3 elements 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..
Using Negative list slicing to remove last K elements of list..
Using Python List pop() Method to remove last K elements of list..

How do I remove something from the end of a list in Python?

The simplest approach is to use the list's pop([i]) function, which removes an element present at the specified position in the list. If we don't specify any index, pop() removes and returns the last element in the list.

How do you pop the last 3 elements of a list in Python?

How to Remove the Last N Elements of a List in Python.
lst = [1, 2, 3, 4] Remove last N elements by slicing. ... .
print(lst[-1]) # 4 print(lst[:-1]) # [1, 2, 3] print(lst) # [1, 2, 3, 4] ... .
lst = lst[:-n] ... .
print(lst[:-0]) # [] ... .
lst = lst[:-n or None] ... .
del lst[-1:] print(lst]) # [1, 2, 3].

How do I remove the last element from a list?

Using del statement You can use del statement to remove last element. Please note that list. pop() returns the remove element, but del statement does not return the removed element. It raises IndexError if list is empty as it is trying to access an index that is out of range.