Question
How can I remove an element from a Python list using its index?
I found list.remove(), but that method searches the list for a matching value. I want to delete an item based on its position in the list instead.
Short Answer
By the end of this page, you will understand how to remove items from a Python list by index, when to use del versus pop(), what happens internally when an item is deleted, and which common mistakes to avoid.
Concept
In Python, a list is an ordered, mutable collection. Because lists keep items in a specific order, every item has an index: 0 for the first item, 1 for the second, and so on.
If you want to remove an item by its position, the main tools are:
del my_list[index]my_list.pop(index)
These are different from:
my_list.remove(value)
remove() deletes the first matching value, not the item at a particular index.
This matters because in real programs, you often know where an item is, not necessarily what exact value it contains. For example:
- deleting the third task in a task list
- removing a row from parsed data
- discarding a menu option at a known position
Also, lists are stored as contiguous sequences of references. When you remove an item from the middle, Python must shift later items left by one position. So removing by index is direct, but it still has a cost when the removed item is not at the end.
Use these rules:
- Use
remove()when you know the value. - Use
pop(index)when you know the index and want the removed item back. - Use
del my_list[index]when you know the index and do not need the removed item.
Mental Model
Think of a list like a row of numbered boxes on a shelf.
remove(value)means: find the box containing this item, then take it out.del my_list[index]means: go directly to box number 3 and remove it.pop(index)means: go directly to box number 3, remove it, and hand me the item you took out.
After removing one box, all the boxes to the right slide left to fill the gap. That is why indexes after the removed item change.
Syntax and Examples
The core ways to remove a list item by index in Python are:
my_list = [10, 20, 30, 40]
del my_list[1]
print(my_list) # [10, 30, 40]
my_list = [10, 20, 30, 40]
removed = my_list.pop(1)
print(removed) # 20
print(my_list) # [10, 30, 40]
del
Use del when you want to delete the item and do not need its value afterward.
names = ["Ana", "Ben", "Cara"]
del names[0]
print(names) # ["Ben", "Cara"]
pop()
Use pop() when you want to delete the item and also keep the removed value.
Step by Step Execution
Consider this example:
colors = ["red", "green", "blue", "yellow"]
removed = colors.pop(2)
print(removed)
print(colors)
Step by step:
-
colorsstarts as:["red", "green", "blue", "yellow"] -
colors.pop(2)removes the item at index2.- index
0->"red" - index
1->"green" - index
2->"blue"
- index
-
The removed value,
"blue", is returned and stored inremoved.
Real World Use Cases
Removing by index appears often in practical code:
- Task managers: remove the task the user selected from a numbered list.
- CSV or parsed data cleanup: delete a known column or row by position.
- Menu systems: remove a disabled option from a list of available actions.
- Games: remove a card from a hand at a selected position.
- Batch processing: discard the item at a specific queue position.
- UI state management: remove the clicked item from an ordered list of elements.
Example: deleting a chosen task
tasks = ["email client", "write report", "deploy app"]
choice = 1
removed_task = tasks.pop(choice)
print(f"Removed: {removed_task}")
print(tasks)
Real Codebase Usage
In real projects, developers usually combine index-based removal with validation and clear control flow.
Guard clauses for safe indexes
def remove_at(items, index):
if index < 0 or index >= len(items):
return None
return items.pop(index)
This avoids IndexError and makes the function predictable.
Input validation
If an index comes from a user, API request, or file, validate it before removing anything.
def delete_user_choice(options, choice_text):
if not choice_text.isdigit():
return "Invalid choice"
index = int(choice_text)
if index < 0 or index >= len(options):
return "Choice out of range"
removed = options.pop(index)
return f"Removed {removed}"
Early returns
Developers often return early when the index is invalid instead of nesting logic deeply.
Common Mistakes
1. Using remove() when you mean index
Broken code:
items = [10, 20, 30]
items.remove(1)
print(items)
This tries to remove the value 1, not the item at index 1.
Correct version:
items = [10, 20, 30]
del items[1]
print(items) # [10, 30]
2. Forgetting that indexes shift after deletion
Broken code:
items = ["a", "b", "c", "d"]
del items[1]
del items[2]
print(items)
You might expect b and c to be removed, but after deleting , the list becomes:
Comparisons
| Method | Removes by | Returns removed item? | Raises error if not found/invalid? | Best use |
|---|---|---|---|---|
del my_list[i] | index | No | Yes, IndexError | Delete by index when you do not need the value |
my_list.pop(i) | index | Yes | Yes, IndexError | Delete by index and keep the removed item |
my_list.pop() | last item | Yes | Yes, if list is empty | Efficiently remove the last item |
my_list.remove(x) |
Cheat Sheet
# Remove by index, no return value
del items[index]
# Remove by index, return removed value
removed = items.pop(index)
# Remove last item
removed = items.pop()
# Remove by value, not index
items.remove(value)
Key rules:
- Lists are zero-indexed.
- Negative indexes count from the end.
deldoes not return a value.pop()returns the removed item.remove()deletes the first matching value.- Invalid index with
delorpop()raisesIndexError. - Missing value with
remove()raisesValueError.
Safe index check:
if 0 <= index < len(items):
items.pop(index)
Remove multiple indexes safely:
for i in sorted(indexes, reverse=True):
items[i]
FAQ
How do I remove an item from a Python list by index?
Use either del my_list[index] or my_list.pop(index). Use pop() if you also want the removed value.
What is the difference between pop() and remove() in Python?
pop() removes by index. remove() removes by value.
How do I delete the last item in a list?
Use my_list.pop() with no argument, or del my_list[-1].
Does removing an item from a list change the indexes?
Yes. After an item is removed, all later items shift left, so their indexes decrease by 1.
What happens if the index does not exist?
Python raises IndexError. You can prevent this by checking the index before deleting.
Which is faster: del, pop(), or remove()?
If you already know the index, del and pop(index) avoid searching by value. must look for a matching value first.
Mini Project
Description
Build a small command-line task list where a user can remove tasks by index. This demonstrates how index-based deletion works in a realistic scenario and reinforces the difference between deleting an item and returning it.
Goal
Create a Python program that shows a list of tasks, removes a task by its index, validates the input, and displays the updated list.
Requirements
- Store several tasks in a Python list.
- Ask the user for the index of the task to remove.
- Validate that the input is a valid integer index.
- Remove the task using index-based deletion.
- Print the removed task and the updated task list.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.