How to Remove Last n Elements from List in Python?

By Hardik Savani October 30, 2023 Category : Python

Hey Artisan,

This article goes in detailed on remove last n elements from list python. If you have a question about how to remove last n elements from list in python then I will give a simple example with a solution. Here you will learn python remove n elements from end of list. you will learn python remove last n element from 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 last in list. Without any further ado, let's see the code examples below.

You can use these examples with python3 (Python 3) version.

Example:

main.py

# Create New List with Item
myList = [1, 2, 3, 4, 5, 6]
  
n = 2
  
# Remove N Number of Item from Last
newList = myList[:len(myList) - n]
  
print(newList)

Output:

[1, 2, 3, 4]

I hope it can help you...

Tags :
Shares