Join Regular Classroom : Visit ClassroomTech

How to delete the first element in List in Python

There are many ways of doing it-
1. pop method – list.pop()
2. remove method – list.remove(list[index])
3. del function – del list[index]
4. slicing – list[1:]

Don’t know how to delete the first element in array in Python? Read: How to delete the first element in array in Python

Here is an example for you

# code to understand how to delete the first element in List in Python
# www.codewindow.in
a = [10, 11, 12, 13, 14]
b = [10, 11, 12, 13, 14]
c = [10, 11, 12, 13, 14]
d = [10, 11, 12, 13, 14]
#using pop method
a.pop(0)
print(a)
#using remove method
b.remove(b[0])
print(b)
#using del method
del c[0]
print(c)
#using slicing
d=d[1:]
print(d)

Output:

[11, 12, 13, 14]
[11, 12, 13, 14]
[11, 12, 13, 14]
[11, 12, 13, 14]

Explanation:
Here, pop(0) pops the first element from the list.
remove(b[0]) removes the element indexed at 0 i.e. the first element.
del c[0] deletes the first element indexed at 0 i.e. the first element.
d[ 1:] slices the list from index 1 leaving the first element at 0.


You Missed

Also Checkout