Concepts / Slicing Lists

Slicing Lists

Removing an element from a list is not one single operation.

  • 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 Question

Removing an element from a list is not one single operation. Python provides pop, del, and remove because each answers a different question. Do you know where the element is, or only what the element is? Do you need the removed value afterward, or can it be discarded? Answering those questions first makes the correct technique easier to choose.

ToolWhat you provideWhat it removesWhat it returns
popAn indexThe element at that indexThe removed value
delAn index or sliceOne element or a range of elementsNo value
removeA valueThe first occurrence of that valueNone

The three main list-removal tools solve different removal problems.

Indexing the List

Both pop and del use positions, or indices. In the list ['a', 'b', 'c'], index 1 identifies 'b'. Removing that element causes the list to shrink, so 'c', which was at index 2, shifts left to index 1. A negative index can also identify an element from the end of a list, while a slice identifies a range of positions.

index 0'a'index 1'b'index 2'c'index -1'c'
Which element does a given index identify before removal?
Code walkthrough
items = ['a', 'b', 'c']
x = items.pop(1)
  1. 1
    The list begins as ['a', 'b', 'c'].
  2. 2
    pop(1) targets index 1, whose value is 'b'.
  3. 3
    The list becomes ['a', 'c'].
  4. 4
    The variable x receives the returned value 'b'.
  • items starts with three elements.
  • Index 1 identifies 'b'.
  • pop removes 'b' and returns it to x.
Output
x is 'b'
items is ['a', 'c']

Tracing Each Removal Tool

Use pop when you know the index and need the removed element. It removes that element, shortens the list, and returns the removed value. Use del when you know the index but do not need the removed value. del can also remove a range through a slice. Use remove when you know the value but not its index. It searches for that value and deletes its first occurrence.

popindex; returns valuelist elementdelindex or slice; no valueremovevalue; returns Noneremovesremovesremoves first match
How do pop, del, and remove differ in their input, result, and return behavior?
Code walkthrough
t = ['a', 'b', 'c']
del t[1]
  1. 1
    The list begins as ['a', 'b', 'c'].
  2. 2
    del t[1] removes 'b'.
  3. 3
    The list becomes ['a', 'c'].
  4. 4
    There is no returned value to store.
  • t[1] identifies 'b'.
  • del removes the element at that index.
  • No removed value is produced for assignment.
Code walkthrough
t = ['a', 'b', 'c', 'd', 'e', 'f']
del t[1:5]
  1. 1
    The selected values are 'b', 'c', 'd', and 'e'.
  2. 2
    The value at index 5, 'f', is outside the slice and remains.
  3. 3
    The list becomes ['a', 'f'].
  4. 4
    After deletion, 'f' shifts to index 1.
  • The slice starts at index 1.
  • The slice stops before index 5.
  • Indices 1, 2, 3, and 4 are removed.
Code walkthrough
t = ['a', 'b', 'c']
t.remove('b')
  1. 1
    The list is searched for the value 'b'.
  2. 2
    The value is found at index 1 and removed.
  3. 3
    The list becomes ['a', 'c'].
  4. 4
    The removed value is not returned.
  • remove receives the value 'b', not an index.
  • The first occurrence of 'b' is deleted.
  • remove returns None.

Watching the List Change

['a', 'b', 'c']['a', 'c']['a', 'b', 'c','d', 'e', 'f']['a', 'f']del index 1del slice 1:5
Which elements remain after removing one indexed element versus a range of indexed elements?

The important state change is not only that an element disappears. Every element after the removed position can receive a new index. After removing 'b' from ['a', 'b', 'c'], 'c' moves from index 2 to index 1. After deleting the slice from index 1 through index 4, 'f' moves from index 5 to index 1.

['a', 'b', 'c']pop(1)['a', 'c']x'b'applyshortens listreturns
How does the removed element move from the list into a variable while the list becomes shorter?

Mistakes Beginners Make

  • Common mistake

    Trying to assign the result of del

    What it looks like
    x = del t[1]
    Why it fails

    del does not produce a value to assign.

    Do this instead

    Use del t[1] when the element should simply be removed. Use x = t.pop(1) when you need the removed value.

  • Common mistake

    Using remove when you need a returned value

    What it looks like
    x = t.remove('b')
    Why it fails

    remove returns None rather than the removed element.

    Do this instead

    Use x = t.pop(index) when you know the index and need to capture the removed element.

  • Common mistake

    Confusing an index with a value

    What it looks like

    Using remove when the task identifies a position, or using pop when the task identifies only a value.

    Why it fails

    pop and del work from indices, while remove searches for a value.

    Do this instead

    Use pop or del for a known index. Use remove for a known value whose position is not known.

  • Common mistake

    Calling remove for a value that is not present

    What it looks like

    Calling t.remove(value) when value does not occur in the list.

    Why it fails

    The requested value cannot be found and the removal operation cannot complete as intended.

    Do this instead

    Consider whether the value may be absent before choosing remove.

  • Common mistake

    Modifying a list while iterating over it

    What it looks like

    Removing elements from a list during iteration.

    Why it fails

    Changing the list during iteration can lead to common bugs.

    Do this instead

    Treat list modification during iteration as an edge case requiring careful handling.

Practice the Decision

MEDIUM

For each situation, choose pop, del, or remove. Then predict the final list and state whether a removed value is available: a) remove the element at index 1 and save it; b) remove the element at index 1 without saving it; c) remove the first occurrence of the value 'b'; d) remove all elements at indices 1 through 4 from ['a', 'b', 'c', 'd', 'e', 'f'].

Hints
  • Use pop when an index is known and the removed value must be captured.
  • Use del for an indexed removal when no returned value is needed.
  • Use remove when the value is known but its index is not.
  • A slice includes its start index and excludes its end index.
Worked example

Selecting the Tool from the Task

A list contains ['a', 'b', 'c']. You know that the target is at index 1, and you need to use the removed value later.

  1. 1
    Identify the information available

    The target is identified by its index, so a value-based search with remove is not the best match.

  2. 2
    Identify the required result

    The removed element must be available later, so the operation must return that element.

  3. 3
    Choose the operation

    pop matches both requirements: it accepts an index and returns the removed value.

  4. 4
    Predict the state

    Index 1 contains 'b'. Removing it leaves ['a', 'c'], and the captured value is 'b'.

Result

Use pop(1). The list becomes ['a', 'c'], and the removed value is 'b'.

Key Takeaways

  1. 1
    Use pop when you know an index and need the removed value.
  2. 2
    Use del when you know an index and do not need a return value, or when you want to remove a range with a slice.
  3. 3
    Use remove when you know the value but not its index; it removes the first occurrence and returns None.
  4. 4
    Removing elements changes later indices because the remaining elements shift left.
  5. 5
    Avoid assigning del, do not expect remove to return the removed value, and consider missing values, invalid positions, and modification during iteration.

At a glance

  • Choose between index-based removal and value-based removal first.
  • pop removes by index and returns the removed element.
  • del removes by index or slice and produces no value to assign.
  • remove searches for and deletes the first occurrence of a value, returning None.
  • After removal, later elements shift to new indices, so predict the list state after every operation.

Related concepts

More concepts starting with S