Remove first two elements in Python List

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[2:]

Here is an example for you-

# code
# www.codewindow.in

a=[100,111,122,132,144]
b=[10,11,12,13,14]
c=['a','s','d','f']
d=["code","window","repeat"]

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

#using remove method
for i in range (2):
    b.remove(b[0])
print(b)

#using del method
for i in range (2):
    del c[0]
print(c)

#using slicing
d=d[2:]
print(d)

Output:

[122, 132, 144]
[12, 13, 14]
['d', 'f']
['repeat']

Explanation:
Here,
pop(0) pops the first element from the list. The for loop executes the block 2 times which means the first element is popped every time the list gets updated.
remove(b[0]) removes the element indexed at 0 i.e. the first element. The for loop executes the block 2 times which means the first element is removed every time the list gets updated (occurs for 2 times).
del c[0] deletes the first element indexed at 0 i.e. the first element. The for loop executes the block 2 times which means the first element is deleted every time the list gets updated (occurs for 2 times).
d[ 2:] slices the array from index 2 leaving the first element at 0 to 1.


Also checkout

Leave a Comment

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