Concepts / Working with Sequences in Python

Working with Sequences in Python

For t[1:3], Python starts at index 1, takes the element there, then continues through index 2.

  • 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

0a1b2c3d4e5fnextincludedstop beforenextnext
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.

Worked example

Reading a Slice Step by Step

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

  1. 1
    Read the start

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

  2. 2
    Read the stop

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

  3. 3
    Collect the selected positions

    The included indices are 2, 3, and 4, which contain 'c', 'd', and 'e'.

Result

['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.

t[:3]first 3 elementst[3:]index 3 onwardt[:]entire list copydifferent boundaryboth omitted
What portion of a list is selected when the start, stop, or both indices are omitted?
Code walkthrough
t = ['a', 'b', 'c', 'd', 'e', 'f']
first_three = t[:3]
from_index_three = t[3:]
whole_copy = t[:]
  1. 1
    The omitted start in t[:3] means selection begins at the start of the list.
  2. 2
    The omitted stop in t[3:] means selection continues from index 3 onward.
  3. 3
    Both omitted indices in t[:] select the entire list as a copy.
  • t[:3] selects the first three elements.
  • t[3:] selects index 3 and every later element.
  • t[:] selects the entire list and creates a copy.
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.

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

Replacing Two Elements with Three

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

  1. 1
    Identify the slice

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

  2. 2
    Remove the selected values

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

  3. 3
    Insert the replacement sequence

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

Result

['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

Definition

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

  • Common mistake

    Including the stop index in the result

    What it looks like
    Expecting t[1:3] to return ['b', 'c', 'd']
    Why it fails

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

    Do this instead

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

  • Common mistake

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

    What it looks like
    Treating t[:3] and t[3:] as equivalent
    Why it fails

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

    Do this instead

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

  • Common mistake

    Assuming slice assignment must preserve list length

    What it looks like

    Expecting t[1:3] = ['x', 'y', 'z'] to leave the list at six elements

    Why it fails

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

    Do this instead

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

  • Common mistake

    Forgetting that slice assignment modifies the original list

    What it looks like

    Using t[1:3] = ['x', 'y'] and expecting a separate result without changing t

    Why it fails

    Slice assignment replaces elements in the original list in place.

    Do this instead

    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.
Code walkthrough
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)
  1. 1
    t[1:4] returns ['b', 'c', 'd'] because index 4 is excluded.
  2. 2
    t[:2] returns ['a', 'b'] because it selects the first two elements.
  3. 3
    t[4:] returns ['e', 'f'] because selection begins at index 4 and continues to the end.
  4. 4
    t[::2] returns ['a', 'c', 'e'] because the step selects every other element.
  5. 5
    t[2:4] selects 'c' and 'd'. Replacing those two elements with ['x'] makes the list shorter.
  • The first expression selects indices 1 through 3.
  • The second expression selects the first two elements.
  • The third expression selects index 4 onward.
  • The fourth expression uses a step of 2.
  • The assignment replaces indices 2 and 3 with one element.
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. 1
    In t[start:stop], start is included and stop is excluded.
  2. 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. 3
    Slice assignment replaces multiple elements in the original list.
  4. 4
    A replacement sequence can make the list grow or shrink when its length differs from the selected slice.
  5. 5
    The full form t[start:stop:step] supports skipped elements, including reverse selection with a negative step.

At a glance

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

Related concepts

More concepts starting with W