How to Remove Item or Clear List in Python?

0
66

remove() method:

The remove() method removes the specified item.

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

pop() method:

The pop() method removes the specified index. If you do not specify the index, the pop() method removes the last item.

thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)

del keyword:

The del keyword also removes the specified index. The del keyword can also delete the list completely.

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

clear() method:

The clear() method empties the list. The list still remains, but it has no content.

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)