Join Regular Classroom : Visit ClassroomTech

Remove first five elements from the list in Python

There are many ways of doing it
1. pop method – list.pop()
2. slicing – list[1:]

Also Read: Python list remove from front

Example:

# code to understand how to remove first five elements from the list in Python
# www.codewindow.in

a = [100, 111, 122, 132, 144, 789, 785, 458, 45]
b = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
c = ['a', 's', 'd', 'f', 'q', 'w', 'e', 'r', 't', 'y']
d = ["code", "window", "repeat", "win", "good", "local", "place"]

#using pop
for i in range (5):
    a.pop(0)
print(a)

#using slicing
b = b[5:]
print(b)

c = c[5:]
print(c)

d = d[5:]
print(d)

Output:

[789, 785, 458, 45]
[15, 16, 17, 18, 19, 20]
['w', 'e', 'r', 't', 'y']
['local', 'place']

Explanation:
Here, pop(0) inside the for loop keeps in poping the index 0, 5 times.
d[ 6:] slices the array from index 5 leaving the element from index 0 to 5.


Follow Us

You Missed

Also Checkout