Tag Archives: pop

Removing an element from a Python list by index

Method 1: Use del

mylist = [i for i in range(1,11)]
del mylist[4]
print mylist
[1, 2, 3, 4, 6, 7, 8, 9, 10]

Method 2: Use pop()

pop() returns the removed element. It takes an optional argument for the element to remove. If this is not supplied, it removes the last element.

mylist = [i for i in range(1,11)]
print mylist.pop()
print mylist
print mylist.pop(4)
print mylist
10
[1, 2, 3, 4, 5, 6, 7, 8, 9]
5
[1, 2, 3, 4, 6, 7, 8, 9]