Join Regular Classroom : Visit ClassroomTech

Remove first element of a list of string and return the list

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 value of array in Python

Example:

# code to understand how to remove first element of a list of string and return the list
# www.codewindow.in

a = ["new", "world"]
b = ["c", "o", "d", "e"]
c = ['a', 's', 'd', 'f']
d = ["code", "window", "repeat"]


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

["world"]
["o", "d", "e"]
['s', 'd', 'f']
["window", "repeat"]

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