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.
Line | Description |
---|---|
1 | Declaration and initialization of the list myList with the values 1, 2, 3, 4 and 5. |
2 | This pop() method removes the last list element. |
3 | This pop(2) method removes the element with index 2. |
5 + 6 | With 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=" ")
1 2 4