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

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.

nextnextnext0a1b2c3d
Which list positions are included in t[1:3], and why is index 3 excluded?

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

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

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.

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

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.

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

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

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

Replace the target: The values 'b' and 'c' are replaced by 'x' and 'y'.

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

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

replaced byshifts rightaab, ct[1:3]x, y, zreplacementd, e, fd, e, f
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

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.

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.

Start at the beginning: The selection begins at the starting boundary.

Move by the step: A step of 2 moves through every other index rather than selecting every adjacent index.

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

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

Mistakes with Slice Boundaries

  • Counting the stop index as part of the result.

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

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

  • Assuming slice assignment only changes a temporary subset.

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

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

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

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

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

  • Forgetting what an omitted boundary means.

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

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

Key Takeaways

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