Concepts / List Mutation and Methods

List Mutation and Methods

List slicing uses the syntax t[start:stop] to extract a portion of a list, where start is included and stop is excluded.

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

Reading Slice Boundaries

A list slice selects a portion of a list with the pattern t[start:stop]. The start index is included, while the stop index is excluded. This means that t[1:3] selects the elements at indices 1 and 2, but not the element at index 3. The most reliable habit is to treat the stop index as a boundary just before that position.

0a1b2c3dnextnextnext
Which list positions are included in t[1:3], and why is index 3 excluded?
Worked example

Selecting the Middle Elements

Given a list whose indexed elements are 0: 'a', 1: 'b', 2: 'c', and 3: 'd', determine the result of t[1:3].

  1. 1
    Read the start

    The start index is 1, so the element at index 1, 'b', is included.

  2. 2
    Read the stop

    The stop index is 3, so selection ends before index 3. The element at index 2, 'c', is included, but the element at index 3, 'd', is not.

  3. 3
    Build the result

    The selected elements are the values at indices 1 and 2.

Result

t[1:3] produces the subset ['b', 'c'].

Omitted Slice Boundaries

Python provides shortcuts when one or both slice boundaries are omitted. The expression t[:n] selects the first n elements, beginning at the start of the list and stopping before index n. The expression t[n:] selects the elements from index n onward, continuing through the end of the list. The expression t[:] selects the entire list and creates a copy of it.

ExpressionSelected portion
t[:n]The first n elements
t[n:]Elements from index n onward
t[:]A copy of the entire list

Short forms created by omitting slice indices.

What do you think happens?

Suppose t contains the indexed values 0: 'a', 1: 'b', 2: 'c', 3: 'd'. What portion does t[2:] select?

Reveal answer
Answer:

It selects the values at index 2 and every index after it: ['c', 'd'].

When the stop index is omitted, selection continues from the given start index through the end of the list.

Best practice

Before evaluating a slice, write down the index positions and mark the start and stop boundaries. Include the start position, stop before the stop position, and then check whether either boundary was omitted.

Changing Lists with Slice Assignment

A slice can appear on the left side of an assignment. With the form t[start:stop] = new_list, the selected portion of the original list is replaced by the values in new_list. Unlike ordinary slicing, which extracts a subset, slice assignment modifies the original list in place. Elements outside the selected range remain unchanged.

ExpressionActionEffect on the original list
t[start:stop]Extracts a portionProduces a subset
t[start:stop] = valuesReplaces a portionMutates the original list

Extraction and slice assignment use similar boundaries but perform different operations.

Worked example

Replacing Two Elements

A list contains the values ['a', 'b', 'c', 'd', 'e', 'f']. Determine the result after assigning ['x', 'y'] to t[1:3].

  1. 1
    Identify the target slice

    The slice t[1:3] covers indices 1 and 2, so it contains 'b' and 'c'.

  2. 2
    Replace the target

    The values 'b' and 'c' are replaced by 'x' and 'y'.

  3. 3
    Preserve the rest

    The values before index 1 and after index 2 remain in the list.

Result

The modified list is ['a', 'x', 'y', 'd', 'e', 'f'].

aab, ct[1:3]x, y, zreplacementd, e, fd, e, freplaced byshifts right
How does assigning three values to a two-element slice change the original list?

The replacement does not have to contain the same number of values as the selected slice. Replacing two elements with three values makes the list grow by one element. Replacing a slice with fewer values makes the list shrink. For example, assigning ['x', 'y', 'z'] to t[1:3] replaces the two values at indices 1 and 2, so the original six-element list becomes a seven-element list. Values after the replaced slice shift to make room for the additional value.

Step-Based Selection

Definition

The full slice form is t[start:stop:step]. The step controls how many elements to skip between selected positions. If the step is omitted, it defaults to 1, so every element in the selected range is included. A step of 2 selects every other element, and a step of 3 selects every third element. A negative step reverses the selection.

Worked example

Applying a Step

For a list with indexed values 0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', determine which positions are considered by a slice with a step of 2.

  1. 1
    Start at the beginning

    The selection begins at the starting boundary.

  2. 2
    Move by the step

    A step of 2 moves through every other index rather than selecting every adjacent index.

  3. 3
    Read the selected values

    The selected positions follow the step pattern until the stop boundary is reached.

Result

A step of 2 selects every other element in the selected range.

Mistakes with Slice Boundaries

  • Common mistake

    Counting the stop index as part of the result.

    What it looks like

    Expecting t[1:3] to include the value at index 3.

    Why it fails

    The start boundary is included, but the stop boundary is excluded.

    Do this instead

    Mark indices 1 and 2 as selected, then stop before index 3.

  • Common mistake

    Assuming slice assignment only changes a temporary subset.

    What it looks like

    Treating t[1:3] = values as if it merely computed a result.

    Why it fails

    An assignment to a slice modifies the original list in place.

    Do this instead

    Track the replacement inside the original list and preserve the elements outside the selected range.

  • Common mistake

    Assuming the replacement must have the same length as the slice.

    What it looks like

    Expecting a two-element slice to accept only two replacement values.

    Why it fails

    The replacement list may be longer or shorter than the slice.

    Do this instead

    Compare the lengths: a longer replacement grows the list, while a shorter replacement shrinks it.

  • Common mistake

    Forgetting what an omitted boundary means.

    What it looks like

    Reading t[:n] as starting at index n.

    Why it fails

    An omitted start selects from the beginning, while an omitted stop continues to the end.

    Do this instead

    Interpret t[:n] as the first n elements and t[n:] as the portion from index n onward.

Prediction Practice

MEDIUM

Consider a list with the indexed values 0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', and 5: 'f'. Predict the result of each operation before checking it through code execution: t[0:2], t[3:], t[:4], and t[1:3] = ['x', 'y', 'z']. For the final operation, predict both the values in the modified list and its new length.

Hints
  • For t[0:2], include index 0 and stop before index 2.
  • For t[3:], begin at index 3 and continue to the end.
  • For t[:4], begin at the start and stop before index 4.
  • For the assignment, replace the two values at indices 1 and 2 with three values.

The final prediction illustrates the two questions that matter most in slice assignment: which positions are replaced, and how many replacement values are inserted. If the replacement is longer than the selected slice, the list grows; if it is shorter, the list shrinks.

Key Takeaways

  1. 1
    A slice uses t[start:stop], includes start, and excludes stop.
  2. 2
    Omitting the start selects from the beginning; omitting the stop selects through the end; t[:] creates a copy of the entire list.
  3. 3
    Slice assignment replaces a selected portion of the original list in place.
  4. 4
    A replacement list can be longer or shorter than the selected slice, causing the list to grow or shrink.
  5. 5
    The third slice parameter, step, controls how many positions are skipped, and a negative step reverses the selection.

At a glance

  • Start-inclusive and stop-exclusive boundaries determine exactly which list elements a slice selects.
  • Omitted boundaries provide shortcuts for selecting from the beginning, through the end, or across the whole list.
  • Slice assignment mutates the original list rather than merely producing a subset.
  • The length of a list can change when the replacement sequence has a different length from the selected slice.
  • Predicting index boundaries before execution is a practical way to debug slice operations.

Related concepts

More concepts starting with L