pop() – delete list entry by index (pop)

Another way to remove list items is to use the pop() method. Either without specifying the index, then the last list element is deleted. If an index is specified, the specified element is deleted.

Code Explanation

LineDescription
1Declaration and initialization of the list myList with the values 1, 2, 3, 4 and 5.
2This pop() method removes the last list element.
3This pop(2) method removes the element with index 2.
5 + 6With the for loop each list element is output.
myList = [1, 2, 3, 4, 5]
myList.pop()
myList.pop(2)

for i in myList:
    print(i, end=" ")
Output
1 2 4