Remove item at index without pop in Python

Remove an item at a given index from a list without using pop by slicing the list around the index.

Easy Python 3.9+ Aug 9, 2026 Algorithms & data structures 12 views 0 copies

Python code

12 lines
Python 3.9+
def remove_at_index(lst, index):
    """Remove item at index and return the new list."""
    if index < 0 or index >= len(lst):
        raise IndexError("Index out of range")
    return lst[:index] + lst[index + 1:]


if __name__ == "__main__":
    items = [10, 20, 30, 40, 50]
    result = remove_at_index(items, 2)
    print(f"Original: {items}")
    print(f"After removing index 2: {result}")

Output

stdout
Original: [10, 20, 30, 40, 50]
After removing index 2: [10, 20, 40, 50]

How it works

This function creates a new list by concatenating the slice before the index and the slice after the index. Slicing is a shallow copy, so integers and other immutable elements are preserved. The original list is not modified, which is useful when you need to keep the input unchanged. The index validation raises an IndexError to match Python's built-in behavior. This method is O(n) because slicing copies the remaining elements. It is a clean, readable pattern for functional-style list manipulation.

Common mistakes

  • Forgetting to validate the index before slicing, which can silently produce wrong results.
  • Modifying the original list in-place when the function is expected to return a new list.
  • Confusing this with pop(), which removes and returns the element from the original list.

Variations

  1. Use del lst[index] to modify the list in-place without returning the removed element.
  2. Use list comprehension with enumerate to filter out the index.

Real-world use cases

  • Removing an item from a list of config options while preserving the original list for rollback.
  • Filtering out an outlier from a data array during preprocessing without altering the source.
  • Building a new list of tasks when skipping one item in a UI queue based on its position.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Algorithms & data structures

Related tutorials and quizzes for this topic.