Concepts / Indexing Lists with the Index Operator

Indexing Lists with the Index Operator

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 Slice Is a Range of Positions

A list slice extracts a portion of a list by specifying a start position and a stop position. The syntax is t[start:stop]. The start index belongs to the selected portion, but the stop index does not. This start-included, stop-excluded rule lets you describe exactly where a selection begins and where it ends.

Tracing t[1:3]

0a1b2c3d4e
Which list positions are included in t[1:3], and why is the start index included while the stop index is excluded?

Selecting the middle elements

Given t = ['a', 'b', 'c', 'd', 'e'], determine the result of t[1:3].

Read the start: The start index is 1, so the selection begins with the element at index 1, which is 'b'.

Read the stop: The stop index is 3, so the element at index 3, 'd', is not included.

Collect the selected positions: The selected indices are 1 and 2, containing 'b' and 'c'.

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

python
Output
['b', 'c']

Omitted Slice Boundaries

A slice does not require both boundary indices. If the start is omitted, the slice begins at the beginning of the list. If the stop is omitted, the slice continues from the given start index onward. If both are omitted, t[:] creates a copy of the entire list.

different boundary omittedboth boundaries omittedt[:3]indices 0 through 2t[3:]index 3 onwardt[:]entire list
What portion of the list is selected when the start index, stop index, or both are omitted?
ExpressionMeaning
t[:n]The first n elements
t[n:]Elements from index n onward
t[:]A copy of the entire list

Shortcuts created by omitting slice indices

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

Replacing a Slice In Place

Slice assignment uses the form t[start:stop] = new_list. Instead of extracting elements, this operation replaces the selected slice in the original list. For example, t[1:3] selects the elements at indices 1 and 2, so assigning ['x', 'y'] replaces those two elements while the other elements remain unchanged.

unchangedreplacedreplacedunchangedaindex 0aunchangedbindex 1xreplacementcindex 2yreplacementdindex 3dunchanged
Which existing elements are replaced by t[1:3] = ['x', 'y'], and how does the list look before and after the change?

t = ['a', 'b', 'c', 'd'] t[1:3] = ['x', 'y'] print(t)

When the List Changes Size

The replacement list does not have to be the same length as the slice being replaced. A longer replacement makes the original list grow, while a shorter replacement makes it shrink. In t[1:3] = ['x', 'y', 'z'], two selected elements are replaced by three elements, so the list gains one element. Elements after the replaced slice shift to make room.

retainedreplacedreplacedshiftedshiftedshiftedabeforeaafterbreplacedxreplacementcreplacedyreplacementdbeforezreplacementebeforedshifted rightfbeforeeshifted rightfshifted right
What changes when a two-element slice is replaced by a three-element list?
python
Output
['a', 'x', 'y', 'z', 'd', 'e', 'f']

Steps Beyond the Basic Slice

The full slice syntax is t[start:stop:step]. If 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']

Common Slice Mistakes

  • Including the stop index in the result

    The stop index is excluded. The slice includes indices 1 and 2 only.

    Fix: Read the expression as starting at index 1 and ending immediately before index 3.

  • Treating t[:n] as including index n

    t[:n] selects the first n elements, which are the positions before index n.

    Fix: For t[:3], count positions 0, 1, and 2.

  • Assuming slice assignment must preserve list length

    The replacement has three elements, while the selected slice has two.

    Fix: Compare the replacement length with the selected slice length. A longer replacement grows the list.

  • Forgetting that slice assignment modifies the original list

    Slice assignment replaces elements in the original list.

    Fix: Inspect the list after the assignment; elements outside the slice remain, while selected elements are replaced.

Predict Before Running

What do you think happens?

What does this code print?

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

Answer: ['b', 'c']

The slice starts at index 1 and stops before index 3, so it selects indices 1 and 2.

python
MEDIUM

For t = ['a', 'b', 'c', 'd', 'e', 'f'], predict the result of t[:2], t[4:], and t[::2]. Then run the expressions and compare the outputs with your predictions.

Hints
  • For t[:2], use the first two elements.
  • For t[4:], begin at index 4 and continue to the end.
  • For t[::2], use a step of 2 and begin at the start because the start and stop are omitted.
HARD

Start with t = ['a', 'b', 'c', 'd', 'e']. Predict the final list after t[1:4] = ['x']. Then verify it by running the code. Explain why the list becomes shorter.

Hints
  • The selected slice contains the elements at indices 1, 2, and 3.
  • The replacement list contains one element.
  • A shorter replacement causes the original list to shrink.

Slice Checklist

  1. Write the slice as start, stop, and optional step.
  2. Identify the element at the start index, if a start is present.
  3. Stop immediately before the stop index, if a stop is present.
  4. If a boundary is omitted, apply the corresponding beginning or end shortcut.
  5. For slice assignment, count the selected elements and compare that count with the replacement list.
  6. Predict the result before running the code, then execute the code to verify the prediction.

Key Takeaways

  • List slicing uses t[start:stop], includes start, and excludes stop.
  • Omitting start selects from the beginning, omitting stop selects through the end, and t[:] creates a copy of the entire list.
  • Slice assignment replaces multiple elements in the original list.
  • A replacement list can have a different length, causing the original list to grow or shrink.
  • The optional step controls how many elements are skipped, and predicting slices before execution strengthens understanding.