Remove the Last N element from a List in Python

In this post, we will learn about different ways to remove last n element from a list in python.

In python, a list is a data type in which we can store multiple mutable or changeable, ordered sequences of elements in a single variable.

And deletion and insertion of elements/items is an important operation we should know while working with a python list.

Here we will see how to delete elements from the end of a list using python.

To remove the last n element from a Python List we can use:

  1. List slicing
  2. del function, or
  3. pop() method

Let’s understand it with some examples.

Remove N element from list using List Index Slicing

To extract any element from an existing list we can use list slicing in python.

We can get a sub-list from the existing list after we have removed the last n elements from it.

The syntax of slicing is:

my_list[start:end:step]

start: the index from which you want to extract the elements.

end: the end index, up to which you want to extract.

Now let’s see an example in which we will remove the last 2 elements from the list.

my_list = ['a', 'b', 'c', 'd', 'e']

#num of elements we want to remove
n = 2

print(my_list[:-n])

Output:

['a', 'b', 'c']

When the start option is undefined, it is considered as 0 index.

Here in the example, we set n to 2 and used my_list[:-2], which means extract all the elements starting from index 0, but do not include the last two elements.

Here, we have used negative index i.e we count the index of the element from backward, here -2 means the element “d“.

Now if you don’t want to use the negative index as the end option in slicing then we can use the len() function, like this

my_list = ['a', 'b', 'c', 'd', 'e']

n = 2

list_len = len(my_list)

end_index = list_len - n

print(my_list[:end_index])
#output: ['a', 'b', 'c']

Here, we have first got the length of the list using len(my_list), and then using list_len - n, we have minus the number of elements we want to remove from the end of the list

Next, we removed the last 2 elements from the list using my_list[:end_index].

Remove last n elements from the list using del function

The del function in python is used to delete objects. And since in python everything is an object so we can delete any list or part of the list with the help of del keyword.

To delete the last element from the list we can just use the negative index,e.g -2 and it will remove the last 2 elements from the list without knowing the length of the list.

my_list = ['a', 'b', 'c', 'd', 'e']

del my_list[-2:]
print(my_list)
#output: ['a', 'b', 'c']

Note: negative index means we count the index of element from backward, .eg my_list[-1] is “e” and my_list[-2] is “d”.

We can also use the len() function to remove the last n element from the list without using any negative index.

Example:

my_list = ['a', 'b', 'c', 'd', 'e']
n=2

del my_list[len(my_list)-n:]

print(my_list)
#output: ['a', 'b', 'c']

Here, the total length of the list is 5, and minus 2 from it, we get 3.

Next, we use the list slicing and passed len(my_list)-n (which is 3) as the start option in my_list[len(my_list)-n:] and kept end option undefined.

So it will delete the elements starting from index 3 upto the end of the list

Remove last element from a list in python using pop()

We can remove any element at a specific index using pop() function. We just have to pass the index of the element and it will remove the element from the python list.

Example:

my_list = ['a', 'b', 'c', 'd', 'e']

my_list.pop(3)
print(my_list)
#output: ['a', 'b', 'c', 'e']

However, if we do not pass any argument in the pop() function, it will remove the last element from a list.

Example:

my_list = ['a', 'b', 'c', 'd', 'e']

my_list.pop()
print(my_list)

Output:

['a', 'b', 'c', 'd']

Conclusion: In this article, we have learned to remove the last n element from a python list using list slicing, del statement, and pop() method. Among all three ways, list slicing and del are more suitable for the removal task, as they let us remove multiple elements at a time, whereas with pop() we can only remove one item at a time.


Related Topics:

Get the Index or Position of Item in List in Python

How to split a list into multiple list using python

Remove Duplicates from List in Python

Prepend List in Python

Python – Insert an element at specific index in a list

Python – Check if list is empty or not

Remove the Last N element from a List in Python

Scroll to Top