29-Mar-2023
.
Admin
Hi Dev,
we will discuss removing the last n elements from the list in the python example. This tutorial will give you a simple example of removing the last n elements from the list python. I explained simply how to remove the last n elements from the list in python. Here you will learn python to remove n elements from the end of the list. So, let's follow a few steps to create an example of python removing the last n element from the list.
There are several ways to remove the last n numbers of elements from the list in python. we will use len() to delete n elements from the last in the list. Without any further ado, let's see the code examples below.
Example :
main.py
# Create New List with Item
myList = [7, 8, 9, 10, 11, 12]
n = 2
# Remove N Number of Item from Last
newList = myList[:len(myList) - n]
print(newList)
Output:
[7, 8, 9, 10]
#Python