How to Remove First Two or More Elements from List in Python?

21-Apr-2023

.

Admin

How to Remove First Two or More Elements from List in Python?

Hi Dev,

In this example, I will show you how to remove the first two or more elements from the list in Python. this example will help you Python remove the first 10 elements from the list. Here you will learn python to delete the first 4 elements from the list. We will use how to remove the first 2 elements from the list in Python.

I will give you examples of how to remove the first two, four elements from the list. you can view one by one example that way you can use what you need. we will use [:numberOfElement] to delete the first elements. let's see the examples:

Example 1: Python Remove First 2 Elements from List


main.py

# Create New List with Item

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

n = 2

# Python Remove First 2 Elements from List

newList = myList[n:]

print(newList)

Output:

[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Example 2: Python Remove First 5 Elements from List

main.py

# Create New List with Item

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

n = 5

# Python Remove First 5 Elements from List

newList = myList[n:]

print(newList)

Output:

[6, 7, 8, 9, 10, 11, 12]

#Python