Since Python lists are mutable (changeable), you can delete elements you no longer need.
There are three main ways to delete elements. You need to choose the right one based on “which element you want to delete” (by position or value) and “if you want to get the deleted value.”
This article explains the functions of the del statement, .remove() method, and .pop() method.
1. del statement – Delete by Index (Position)
del is a Python statement. It completely removes the element at the specified index (position number) from the list.
Use this when you know the position, regardless of the value. The deleted element is not returned; it is simply erased.
Syntax:
del list_variable[index]
Example:
# User list
active_users = ["Sato", "Suzuki", "Takahashi", "Watanabe"]
print(f"Before: {active_users}")
# Delete the 2nd item (Index 1) "Suzuki"
del active_users[1]
print(f"After: {active_users}")
Output:
Before: ['Sato', 'Suzuki', 'Takahashi', 'Watanabe']
After: ['Sato', 'Takahashi', 'Watanabe']
“Suzuki” at active_users[1] was deleted, and the subsequent elements shifted forward.
2. .remove() method – Delete by Value
.remove() deletes an element by specifying the value itself, not the index.
Use this when you know the value you want to delete but not its position.
Important Notes:
- If the value exists multiple times, only the first occurrence (the one with the lowest index) is deleted.
- If the value does not exist, a
ValueErroroccurs.
Syntax:
list_variable.remove(value_to_delete)
Example:
# List with duplicate tasks
task_list = ["Data Check", "Meeting", "Send Email", "Data Check"]
print(f"Before: {task_list}")
# Remove the value "Data Check"
task_list.remove("Data Check")
print(f"After: {task_list}")
Output:
Before: ['Data Check', 'Meeting', 'Send Email', 'Data Check']
After: ['Meeting', 'Send Email', 'Data Check']
Only the first “Data Check” at index 0 was deleted. The one at the end remains.
3. .pop() method – Delete by Index (and Get Value)
Like del, .pop() deletes an element by specifying the index. The biggest difference is that it returns the deleted element.
This is very useful when you want to take an element out of the list and use its value in another variable (e.g., implementing queues or stacks).
Syntax:
deleted_value = list_variable.pop(index)
Important Feature:
If you omit the index and call list_variable.pop(), the last element of the list is deleted and returned.
Example:
# Queue waiting for processing
pending_queue = ["Job-A", "Job-B", "Job-C", "Job-D"]
print(f"Queue (Before): {pending_queue}")
# 1. Take out the first element (Index 0)
processed_job = pending_queue.pop(0)
print(f"Processed Job: {processed_job}")
print(f"Queue (After): {pending_queue}")
# 2. Take out the last element (Index omitted)
last_job = pending_queue.pop()
print(f"Last Job: {last_job}")
print(f"Queue (Final): {pending_queue}")
Output:
Queue (Before): ['Job-A', 'Job-B', 'Job-C', 'Job-D']
Processed Job: Job-A
Queue (After): ['Job-B', 'Job-C', 'Job-D']
Last Job: Job-D
Queue (Final): ['Job-B', 'Job-C']
Summary: How to Choose
| Method | Specify By | Return Value | Feature |
del | Index | None | Deletes by position simply. |
.remove() | Value | None | Deletes by searching for value. Only the first one. Error if missing. |
.pop() | Index (Last if omitted) | Yes | Gets the deleted value for reuse. |
- Delete by position, value not needed ->
del - Delete by value ->
.remove() - Delete by position and use the value ->
.pop()
