Removing Items from a Python List

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

Using remove()

Example: Removing an Item by Value

# Using remove()
fruits = ["Apple", "Banana", "Cherry", "Date"]
print("Original List:", fruits)
fruits.remove("Banana")
print("After Remove:", fruits)

Output

Original List: ['Apple', 'Banana', 'Cherry', 'Date']

After Remove: ['Apple', 'Cherry', 'Date']

Explanation: The remove() method removes the first occurrence of the specified value.

Using pop()

Example: Removing an Item by Index

# Using pop()
numbers = [10, 20, 30, 40, 50]
print("Original List:", numbers)
removed_item = numbers.pop(2)
print("After Pop:", numbers)
print("Removed Item:", removed_item)

Output

Original List: [10, 20, 30, 40, 50]

After Pop: [10, 20, 40, 50]

Removed Item: 30

Explanation: The pop() method removes the item at the specified index and returns it.

Using del

Example: Deleting an Item or Entire List

# Using del
names = ["Karthick AG", "Durai", "Vijay", "John"]
print("Original List:", names)
del names[1]
print("After Deletion:", names)
del names  # Deletes the entire list

Note: The last line deletes the entire list. Attempting to print names after this will result in an error.