Remove List Items

You can remove items from a list using methods like remove(), pop(), or by using the del statement.

Using remove():

The remove() method removes the first occurrence of a specified item:

Python
fruits = ["apple", "banana", "cherry", "banana"]

# Remove "banana"
fruits.remove("banana")

print(fruits)  # Output: ["apple", "cherry", "banana"]

Using pop():

The pop() method removes an item at a specified index (or the last item if no index is specified):

Python
fruits = ["apple", "banana", "cherry"]

# Remove the second item
fruits.pop(1)

print(fruits)  # Output: ["apple", "cherry"]

# Remove the last item
last_item = fruits.pop()
print(last_item)  # Output: cherry
print(fruits)  # Output: ["apple"]

Using del Statement:

You can use the del statement to delete an item at a specified index or delete the entire list:

Python
fruits = ["apple", "banana", "cherry"]

# Delete the first item
del fruits[0]

print(fruits)  # Output: ["banana", "cherry"]

# Delete the entire list
del fruits

# This will raise an error since the list no longer exists
# print(fruits)