Remove first tuple in a list 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:]

Also Read: Write a program in Python to delete first element from a list

Example:

# code to understand how to remove first tuple in a list Python
# www.codewindow.in

a = [(100, 111), (122, 132), 144]
b = [(10, 11, 12), (13, 14), (87, 29)]
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:

[(122, 132), 144]
[(13, 14), (87, 29)]
[('d', 'f')]
['repeat']

Explanation:
Here, pop(0) pops the first element from the list i.e the first tuple inside the list “(100, 111)”.
remove(b[0]) removes the element indexed at 0 i.e. the first tuple inside the list “(10, 11, 12)”.
del c[0] deletes the first element indexed at 0 i.e. the first tuple inside the list “(‘a’, ‘s’)”.
d[ 1:] slices the array from index 1 leaving the first tuple at index 0 i.e.(“code”, “window”).


Follow Us

You Missed

Also Checkout