Concepts / Working with Sequences in Python

Working with Sequences in Python

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

  • Programming

A Boundary That Matters

A list slice looks small, but its boundaries control exactly which elements are selected. In the expression t[start:stop], the start index is included and the stop index is excluded. That single rule lets you select a precise portion of a list and is also the basis for changing several list elements at once.

What do you think happens?

Given the list ['a', 'b', 'c', 'd', 'e', 'f'], which elements does t[1:3] select?

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

Answer: ['b', 'c']

The slice begins at index 1 and includes that index. It stops before index 3, so it includes indices 1 and 2 only.

Mapping Boundaries to Elements

nextincludedstop beforenextnext0a1b2c3d4e5f
Which list elements are included when a slice uses start index 1 and stop index 3?

For t[1:3], Python starts at index 1, takes the element there, then continues through index 2. It stops before index 3. With t equal to ['a', 'b', 'c', 'd', 'e', 'f'], the result is ['b', 'c']. The stop value is a boundary rather than an included position.

Reading a Slice Step by Step

Determine the result of t[2:5] when t is ['a', 'b', 'c', 'd', 'e', 'f'].

Read the start: The start index is 2, so the slice begins with 'c'.

Read the stop: The stop index is 5, so index 5 and its value 'f' are not included.

Collect the selected positions: The included indices are 2, 3, and 4, which contain 'c', 'd', and 'e'.

['c', 'd', 'e']

Shortcuts for List Portions

The start and stop indices are optional. Writing t[:n] selects the first n elements, because the slice begins at the start of the list and stops before index n. Writing t[n:] selects the elements from index n onward. Writing t[:] selects the entire list and creates a copy of it.

different boundaryboth omittedt[:3]first 3 elementst[3:]index 3 onwardt[:]entire list copy
What portion of a list is selected when the start, stop, or both indices are omitted?

t = ['a', 'b', 'c', 'd', 'e', 'f'] first_three = t[:3] from_index_three = t[3:] whole_copy = t[:]

Output
first_three = ['a', 'b', 'c']
from_index_three = ['d', 'e', 'f']
whole_copy = ['a', 'b', 'c', 'd', 'e', 'f']

Replacing a Slice

A slice can appear on the left side of an assignment. The form t[start:stop] = new_list replaces every element in the selected slice with the elements of new_list. This modifies the original list in place. Elements outside the selected slice remain unchanged, while the list can grow or shrink if the replacement has a different length.

t[1:3] = ['x', 'y', 'z']['a', 'b', 'c', 'd','e', 'f']6 elements['a', 'x', 'y', 'z','d', 'e', 'f']7 elements
How does assigning three replacement elements to a two-element slice change the original list and its length?

Replacing Two Elements with Three

Apply t[1:3] = ['x', 'y', 'z'] to t = ['a', 'b', 'c', 'd', 'e', 'f'].

Identify the slice: Indices 1 and 2 are selected. Their values are 'b' and 'c'; index 3 is excluded.

Remove the selected values: The two selected elements are replaced, while 'a', 'd', 'e', and 'f' stay in the list.

Insert the replacement sequence: The three replacement values 'x', 'y', and 'z' occupy the slice position.

['a', 'x', 'y', 'z', 'd', 'e', 'f']

python
Output
['a', 'x', 'y', 'z', 'd', 'e', 'f']

The original slice contains two elements, but the replacement contains three. Therefore, the list grows from six elements to seven. If the replacement list were shorter than the selected slice, the list would shrink instead.

Extending the Mental Model

The full slice syntax is t[start:stop:step]. The step controls how many elements to skip between selections. 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.

python
Output
['a', 'c', 'e']
['a', 'd']

Mistakes Beginners Make

  • Including the stop index in the result

    The stop index is excluded, so index 3 and the value at that position are not selected.

    Fix: Treat t[1:3] as the range containing indices 1 and 2.

  • Confusing t[:n] with t[n:]

    The first form selects the first n elements, while the second starts at index n and continues onward.

    Fix: Use t[:n] for the beginning portion and t[n:] for the portion beginning at index n.

  • Assuming slice assignment must preserve list length

    The replacement contains three elements while the selected slice contains two.

    Fix: Compare the replacement length with the selected slice length. A longer replacement grows the list, and a shorter replacement shrinks it.

  • Forgetting that slice assignment modifies the original list

    Slice assignment replaces elements in the original list in place.

    Fix: Expect t itself to contain the replacement values after the assignment.

Predict Before Running

MEDIUM

For t = ['a', 'b', 'c', 'd', 'e', 'f'], predict the result of each expression before checking it in Python: t[1:4], t[:2], t[4:], and t[::2]. Then predict the final list after t[2:4] = ['x'].

Hints
  • For each two-index slice, include the start index and stop before the stop index.
  • An omitted start begins at the beginning of the list.
  • An omitted stop continues to the end of the list.
  • A step of 2 selects every other element.
  • For slice assignment, compare the number of selected elements with the number of replacement elements.

t = ['a', 'b', 'c', 'd', 'e', 'f'] print(t[1:4]) print(t[:2]) print(t[4:]) print(t[::2]) t[2:4] = ['x'] print(t)

Output
['b', 'c', 'd']
['a', 'b']
['e', 'f']
['a', 'c', 'e']
['a', 'b', 'x', 'e', 'f']

The final check confirms both parts of the mental model: t[2:4] selects indices 2 and 3, and replacing two elements with one causes the list to shrink. Prediction is useful because it exposes an incorrect boundary assumption before that assumption causes a harder debugging problem.

Key Takeaways

  1. In t[start:stop], start is included and stop is excluded.
  2. t[:n] selects the first n elements, t[n:] selects from index n onward, and t[:] creates a copy of the entire list.
  3. Slice assignment replaces multiple elements in the original list.
  4. A replacement sequence can make the list grow or shrink when its length differs from the selected slice.
  5. The full form t[start:stop:step] supports skipped elements, including reverse selection with a negative step.

Key Takeaways

  • A slice includes its start index but excludes its stop index.
  • Omitted boundaries provide shortcuts for selecting the beginning, end, or entire list.
  • Slice assignment changes the original list and can change its length.
  • Predicting slice results before execution strengthens understanding of index boundaries.
  • The optional step selects elements at intervals and can reverse selection when negative.