Question

How to Remove an Element from a List by Index in Python

pythonlistindexing

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.

Take Quiz

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.

names = ["Ana", "Ben", "Cara"]
first_name = names.pop(0)
print(first_name)  # Ana
print(names)       # ['Ben', 'Cara']

Negative indexes

Python also supports negative indexes.

numbers = [5, 6, 7, 8]
last = numbers.pop(-1)
print(last)     # 8
print(numbers)  # [5, 6, 7]

Removing the last item

If you want the last item, pop() can be called with no argument.

items = ["a", "b", "c"]
last_item = items.pop()
print(last_item)  # c
print(items)      # ['a', 'b']

Step by Step Execution

Consider this example:

colors = ["red", "green", "blue", "yellow"]
removed = colors.pop(2)
print(removed)
print(colors)

Step by step:

  1. colors starts as:

    ["red", "green", "blue", "yellow"]
    
  2. colors.pop(2) removes the item at index 2.

    • index 0 -> "red"
    • index 1 -> "green"
    • index 2 -> "blue"
  3. The removed value, "blue", is returned and stored in removed.

  4. The remaining items shift left:

    ["red", "green", "yellow"]
    
  5. print(removed) outputs:

    blue
    
  6. print(colors) outputs:

    ['red', 'green', 'yellow']
    

Now compare with del:

colors = ["red", "green", "blue", "yellow"]
del colors[2]
print(colors)

This also removes "blue", but nothing is returned.

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)
Take Quiz

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.

Removing multiple items

When deleting several indexes, developers often process indexes in reverse order so earlier deletions do not shift later positions.

items = ["a", "b", "c", "d", "e"]
indexes_to_remove = [1, 3]

for i in sorted(indexes_to_remove, reverse=True):
    del items[i]

print(items)  # ['a', 'c', 'e']

Error handling

If an invalid index should be considered exceptional, code may catch IndexError.

try:
    removed = items.pop(10)
except IndexError:
    print("Index does not exist")

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 b, the list becomes:

['a', 'c', 'd']

Then del items[2] removes d.

Safer approach:

items = ["a", "b", "c", "d"]
for i in [2, 1]:
    del items[i]
print(items)  # ['a', 'd']

3. Using an out-of-range index

Broken code:

items = [1, 2, 3]
items.pop(5)

This raises:

IndexError: pop index out of range

Avoid it with validation:

if 0 <= index < len(items):
    items.pop(index)

4. Expecting del to return a value

Broken code:

items = [1, 2, 3]
removed = del items[1]

del is a statement, not a function. It does not return anything.

Use pop() instead:

items = [1, 2, 3]
removed = items.pop(1)
print(removed)  # 2

5. Mixing up negative indexes

items = ["x", "y", "z"]
print(items.pop(-1))  # z

Negative indexes count from the end:

  • -1 is the last item
  • -2 is the second last

Comparisons

MethodRemoves byReturns removed item?Raises error if not found/invalid?Best use
del my_list[i]indexNoYes, IndexErrorDelete by index when you do not need the value
my_list.pop(i)indexYesYes, IndexErrorDelete by index and keep the removed item
my_list.pop()last itemYesYes, if list is emptyEfficiently remove the last item
my_list.remove(x)valueNoYes, ValueError if value missingDelete first matching value

del vs pop()

  • Choose del for simple deletion.
  • Choose pop() when the removed value matters.

pop(index) vs remove(value)

  • pop(index) uses position.
  • remove(value) uses content.

Last item vs middle item

Removing the last item is usually cheaper because no shifting is needed. Removing from the middle or front requires later elements to move left.

Take Quiz

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.
  • del does not return a value.
  • pop() returns the removed item.
  • remove() deletes the first matching value.
  • Invalid index with del or pop() raises IndexError.
  • Missing value with remove() raises ValueError.

Safe index check:

if 0 <= index < len(items):
    items.pop(index)

Remove multiple indexes safely:

for i in sorted(indexes, reverse=True):
    del 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. remove() must look for a matching value first.

Can I use negative indexes with pop()?

Yes. For example, pop(-1) removes the last item.

Should I use del or pop()?

Use del if you only want to delete. Use pop() if you want to keep the removed item.

Take Quiz

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.
Take Quiz

Keep learning

Related questions

View more