Concepts / Slicing Lists

Slicing Lists

pop removes an element by index and returns its value; use it when you need to capture the removed element.

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

items = ['a', 'b', 'c'] x = items.pop(1)

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.

removesremovesremoves first matchpopindex; returns valuelist elementdelindex or slice; no valueremovevalue; returns None
How do pop, del, and remove differ in their input, result, and return behavior?

t = ['a', 'b', 'c'] del t[1]

t = ['a', 'b', 'c', 'd', 'e', 'f'] del t[1:5]

t = ['a', 'b', 'c'] t.remove('b')

Watching the List Change

del index 1del slice 1:5['a', 'b', 'c']['a', 'c']['a', 'b', 'c','d', 'e', 'f']['a', 'f']
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.

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

Mistakes Beginners Make

  • Trying to assign the result of del

    del does not produce a value to assign.

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

  • Using remove when you need a returned value

    remove returns None rather than the removed element.

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

  • Confusing an index with a value

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

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

  • Calling remove for a value that is not present

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

    Fix: Consider whether the value may be absent before choosing remove.

  • Modifying a list while iterating over it

    Changing the list during iteration can lead to common bugs.

    Fix: 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.

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.

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

Identify the required result: The removed element must be available later, so the operation must return that element.

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

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

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

Key Takeaways

  1. Use pop when you know an index and need the removed value.
  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. Use remove when you know the value but not its index; it removes the first occurrence and returns None.
  4. Removing elements changes later indices because the remaining elements shift left.
  5. Avoid assigning del, do not expect remove to return the removed value, and consider missing values, invalid positions, and modification during iteration.

Key Takeaways

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