Python - Remove List Items
Remove Specified Item
Theremove()
method removes the specified item.
Example
Remove "banana":
thislist.remove("banana")
print(thislist)
If there are more than one item with the specified value, theremove()
method removes the first occurrence:
Example
Remove the first occurrence of "banana":
thislist.remove("banana")
print(thislist)
Remove Specified Index
Thepop()
method removes the specified index.
Example
Remove the second item:
thislist.pop(1)
print(thislist)
If you do not specify the index, thepop()
method removes the last item.
Example
Remove the last item:
thislist.pop()
print(thislist)
Thedel
keyword also removes the specified index:
Example
Remove the first item:
delthislist[0]
print(thislist)
Thedel
keyword can also delete the list completely.
Clear the List
Theclear()
method empties the list.
The list still remains, but it has no content.
Example
Clear the list content:
thislist.clear()
print(thislist)