Removing a number from the beginning of a list in Python

Methods:
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:]

Also Read: Remove first element of a list of string and return the list

Example:

# code to understand how to remove a number from the beginning of a list in Python
# www.codewindow.in

a = [100, 111, 122, 132, 144]
b = [10, 11, 12, 13, 14]
c = [2, 4, 5]
d = [75, 78, 79, 74]


#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:

[111, 122, 132, 144]
[11, 12, 13, 14]
[4, 5]
[78, 79, 74]

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 array from index 1 leaving the first element at 0.


Follow Us

You Missed

Also Checkout


Leave a Comment

Your email address will not be published. Required fields are marked *