Remove the Last N element from a List in Python
📋 Table Of Content
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:
- List slicing
- del function, or
- 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
start: the index from which you want to extract the elements.
end: the end index, upto 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])
['a', 'b', 'c']
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
Remove last n elements from the list using del function
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
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
We can also use the
len() function to remove the last n element from the list without using any negative index.
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.
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.
my_list = ['a', 'b', 'c', 'd', 'e'] my_list.pop() print(my_list)
['a', 'b', 'c', 'd']
Conclusion: In this article, we have learned to remove the last n element from a python list using
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.
- Check for prime number using for and while loop in Python
- Merge two dictionaries together using python (3 ways)
- Merge multiple JSON objects into one single object in Python
- Python - Check if list is empty or not
- Python - Insert an element at specific index in a list