Concepts / Indexing and Slicing Lists

Indexing and Slicing Lists

The remove() method deletes the first occurrence of a value from a list when you know the value but not its index.

  • Programming
Interactive lab

Try it: Indexing and Slicing

How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.

How it works

  1. Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
  2. An index outside the string raises IndexError.
  3. A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
  4. A negative step walks backwards; a step of 0 is a ValueError.

Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).

Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.

Educational simulation

Loading the simulation…

Choosing the Removal Tool

List deletion depends on what you know. If you know the value you want to remove but not its position, use remove(). If you know the position, use del with an index. If you know a consecutive range of positions, use del with slice notation.

OperationWhat determines the deletionHow many elements it can remove
remove(value)The first matching valueOne element
del list[index]One positionOne element
del list[start:stop]A range of positionsMultiple consecutive elements
remove(value)matches a valuedel list[index]uses one positiondel list[start:stop]uses a position range
How does remove(value) differ from del list[index] or del list[start:stop] in what determines the elements deleted?

Removing a Known Value

The remove() method is for the situation in which you know the value to delete but do not know where that value occurs in the list. It searches for the value and deletes its first occurrence. For example, applying remove('b') to a list containing 'a', 'b', and 'c' deletes 'b', leaving 'a' and 'c'. The list becomes shorter, and the remaining elements shift to fill the gap.

0a0a1b1b2b2c3c
When a value appears more than once, which occurrence does remove() delete?

Reading a Deletion Slice

Deleting the Middle Range

A list contains the six values 'a', 'b', 'c', 'd', 'e', and 'f'. Which values are deleted by del t[1:5], and what remains?

Read the start: The start index is 1, so the selected range begins with the element at index 1, which is 'b'.

Read the stop: The stop index is 5, but the right boundary is exclusive. Index 5 itself is not selected.

List the selected indices: The selected indices are 1, 2, 3, and 4. Their values are 'b', 'c', 'd', and 'e'.

Inspect what remains: The values at indices 0 and 5 remain: 'a' and 'f'. The original element at index 5 becomes index 1 in the shorter list.

del t[1:5] removes 'b', 'c', 'd', and 'e', leaving 'a' and 'f'.

next boundary0a1b2c3d4e5f5stop boundary
Which list elements are removed when del uses a start index and a stop index, and is the stop index included?

For del t[start:end], the start index is included and the end index is excluded. Therefore, del t[1:5] removes indices 1, 2, 3, and 4, not index 5.

Tracking the Shift

Deletion changes the positions of elements that remain after the removed portion. When one element is removed, later elements shift to fill the gap. When a range is removed, the elements after that range shift left by the number of deleted elements. In the six-element example, 'f' originally has index 5. After indices 1 through 4 are deleted, 'f' is at index 1.

shifts leftaindex 0aindex 0bindex 1findex 1cindex 2dindex 3eindex 4findex 5
What does the list look like before and after deleting multiple consecutive elements with del and a slice?
stays at 0moves to 10a0a1f5f
How do the positions of the remaining list elements change after an element or range is removed?

Common Deletion Mistakes

  • Treating remove() as if it removes an index

    remove() searches for the value 2. It does not interpret 2 as a position.

    Fix: Use del list[2] when the position to delete is index 2.

  • Assuming the stop index in a deletion slice is included

    The right slice boundary is exclusive.

    Fix: Read del t[1:5] as deleting indices 1, 2, 3, and 4.

  • Expecting remove() to return the deleted value

    remove() returns None and modifies the list in place.

    Fix: Inspect the list after the call if you need to see the result of the deletion.

  • Continuing to use old positions after deletion

    Remaining elements shift to fill the removed range.

    Fix: Re-evaluate the indices after deletion; in the source example, the original index-5 element becomes index 1.

Before using del with a slice, write out the start index, the stop index, and the indices between them. Then check that the stop index is not part of the deletion. This makes the exclusive right boundary explicit.

Check Your Understanding

MEDIUM

A list contains the values 'a', 'b', 'c', 'd', 'e', and 'f'. Decide which operation fits each goal: remove the first 'd' when its position is unknown; delete the element at index 2; delete the consecutive elements at indices 1, 2, and 3.

Hints
  • Use remove() when the value is known.
  • Use del with one index when one position is known.
  • Use del with a slice whose start is 1 and whose exclusive stop is after index 3.

What do you think happens?

For the list 'a', 'b', 'c', 'd', 'e', 'f', what remains after del t[1:5]?

  • ['a', 'f']
  • ['b', 'c', 'd', 'e']
  • ['a', 'e', 'f']
Reveal answer

Answer: ['a', 'f']

The slice selects indices 1, 2, 3, and 4. Index 5 is the exclusive stop boundary, so 'f' remains.

Key Takeaways

  1. Use remove(value) when you know the value but not its position; it deletes the first occurrence.
  2. remove() modifies the list in place and returns None.
  3. Use del with one index to remove an element by position.
  4. Use del list[start:stop] to remove multiple consecutive elements.
  5. The stop boundary of a slice is exclusive, and elements remaining after deletion shift to new positions.

Key Takeaways

  • remove() deletes the first matching value, not an element at a specified index.
  • del with an index removes one element by position.
  • del with a slice removes a consecutive range in one operation.
  • Slice stops are exclusive, so del t[1:5] removes indices 1 through 4.
  • After deletion, remaining elements shift to fill the gap.