How to Remove an Item from a List in Python

Remove Item From List Python

A Python list is a changeable sequence, so you can add or remove elements after you create it. You can remove specific items or items at certain positions. It helps you keep your lists clean and organized.

What is a Python List?

A Python list is an ordered, changeable collection of items enclosed in square brackets, like [1, 2, 3]. You can store different data types in a single list. You can modify lists after you create them, which includes adding, changing, or removing items.

Academy Pro

Python Programming Course

In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.

11.5 Hrs
51 Coding Exercises
Learn Python Programming

4 Main Ways to Remove an Item from a Python List

You can remove items from a Python list in several ways:

  • Using remove(): Remove a specific item by its value.
  • Using pop(): Remove an item by its index, or the last item.
  • Using del keyword: Remove an item by its index or a slice.
  • Using clear(): Remove all items from the list.

Let’s look at each one.

1. Using remove()

The remove() method takes the value of the item you want to remove. It removes the first occurrence of that value in the list.

  • Syntax: my_list.remove(value)
  • What it does: Removes the first item that matches value.
  • When to use: When you know the item’s value and want to remove it.

Here is an example:

# Create a list of fruits
fruits = ["apple", "banana", "cherry", "banana"]

# Remove the first "banana"
fruits.remove("banana")
print(fruits)

This code outputs:

['apple', 'cherry', 'banana']

Notice that only the first “banana” was removed. If the item is not in the list, remove() will raise a ValueError. You can prevent this error by checking if the item exists first:

my_numbers = [10, 20, 30]

# Try to remove an item that does not exist (will cause an error)
# my_numbers.remove(40) # This would raise a ValueError

# Safely remove an item
if 40 in my_numbers:
    my_numbers.remove(40)
else:
    print("40 is not in the list.")

print(my_numbers)

This code outputs:

40 is not in the list.
[10, 20, 30]

This check prevents your program from crashing.

2. Using pop()

The pop() method removes an item at a specific index. If you do not provide an index, pop() removes and returns the last item in the list.

  • Syntax (with index): my_list.pop(index)
  • Syntax (without index): my_list.pop()
  • What it does: Removes the item at index (or the last item) and returns it.
  • When to use: When you know the item’s position, or you need to remove the last item.

Here are examples:

# Create a list of colors
colors = ["red", "green", "blue", "yellow"]

# Remove the item at index 1 (which is "green")
removed_color = colors.pop(1)
print(f"Removed color: {removed_color}")
print(colors)

# Remove the last item (which is "yellow" after the previous pop)
last_item = colors.pop()
print(f"Removed last item: {last_item}")
print(colors)

This code outputs:

Removed color: green
['red', 'blue', 'yellow']
Removed last item: yellow
['red', 'blue']

If you try to pop() from an empty list or use an invalid index, it will raise an IndexError.

3. Using del Keyword

The del keyword is a Python statement, not a list method. It allows you to delete items from a list by index or even delete slices (multiple items) or the entire list.

  • Syntax (single item): del my_list[index]
  • Syntax (slice): del my_list[start:end]
  • Syntax (entire list): del my_list
  • What it does: Removes item(s) at specified index/slice, or deletes the list variable itself.
  • When to use: When you know the item’s position, need to remove multiple items, or delete the list variable.

Here are examples:

# Create a list of numbers
numbers = [10, 20, 30, 40, 50]

# Delete the item at index 2 (which is 30)
del numbers[2]
print(numbers)

# Delete a slice (items from index 0 up to, but not including, index 2)
numbers_slice = [1, 2, 3, 4, 5, 6]
del numbers_slice[0:2] # Removes 1 and 2
print(numbers_slice)

# Delete the entire list variable
my_data = ["A", "B", "C"]
del my_data
# print(my_data) # This would now raise a NameError because my_data no longer exists

This code outputs for the first two parts:

[10, 20, 40, 50]
[3, 4, 5, 6]

When you use del my_data, the variable my_data is completely removed from your program’s memory. If you try to use it afterward, you will get a NameError.

4. Using clear()

The clear() method removes all items from a list. It makes the list empty, but the list object itself still exists.

  • Syntax: my_list.clear()
  • What it does: Empties the list.
  • When to use: When you want to remove all contents but keep the list structure.

Here is an example:

# Create a list of tasks
tasks = ["buy groceries", "pay bills", "walk dog"]

# Clear all items from the list
tasks.clear()
print(tasks)

This code outputs:

[]

The list tasks is now empty. It is different from del tasks, which would remove the tasks variable entirely.

Best Practices for Removing List Items

Choose the Right Method:

  • Use remove() when you know the value of the item.
  • Use pop(index) when you know the position (index) of the item.
  • Use pop() without an index to process items from the end of the list (e.g., for stacks).
  • Use del for specific index removal or for removing slices.
  • Use clear() when you need to empty the entire list but keep the list variable.
  • Handle Errors: Always check for ValueError when using remove() and IndexError when using pop() with an index or del with an invalid index.
  • Be Aware of Side Effects: Removing items changes the list in place. This can affect loops or other parts of your code that rely on the list’s length or item positions.
Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.

Academy Pro Subscription

Grab 50% off
on Top Courses - Free Trial Available

×
Scroll to Top