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

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?
Worked example

Selecting the middle elements

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

  1. 1
    Read the start

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

  2. 2
    Read the stop

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

  3. 3
    Collect the selected positions

    The selected indices are 1 and 2, containing 'b' and 'c'.

Result

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.

t[:3]indices 0 through 2t[3:]index 3 onwardt[:]entire listdifferent boundary omittedboth boundaries omitted
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.

aindex 0aunchangedbindex 1xreplacementcindex 2yreplacementdindex 3dunchangedunchangedreplacedreplacedunchanged
Which existing elements are replaced by t[1:3] = ['x', 'y'], and how does the list look before and after the change?
Code walkthrough
t = ['a', 'b', 'c', 'd']
t[1:3] = ['x', 'y']
print(t)
  1. 1
    The original list contains 'b' and 'c' at the selected positions.
  2. 2
    The replacement list contains 'x' and 'y'.
  3. 3
    The final list keeps 'a' and 'd' and places the replacement elements between them.
  • The slice t[1:3] identifies the elements at indices 1 and 2.
  • The assignment replaces those selected elements with 'x' and 'y'.
  • The elements outside the slice remain in the list.
Output
['a', 'x', 'y', 'd']

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.

abeforeaafterbreplacedxreplacementcreplacedyreplacementdbeforezreplacementebeforedshifted rightfbeforeeshifted rightfshifted rightretainedreplacedreplacedshiftedshiftedshifted
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

  • Common mistake

    Including the stop index in the result

    What it looks like

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

    Why it fails

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

    Do this instead

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

  • Common mistake

    Treating t[:n] as including index n

    What it looks like

    Expecting t[:3] to contain four elements

    Why it fails

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

    Do this instead

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

  • 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 its original length

    Why it fails

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

    Do this instead

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

  • Common mistake

    Forgetting that slice assignment modifies the original list

    What it looks like

    Using t[1:3] = ['x', 'y'] and then expecting t to remain unchanged

    Why it fails

    Slice assignment replaces elements in the original list.

    Do this instead

    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. 1
    Write the slice as start, stop, and optional step.
  2. 2
    Identify the element at the start index, if a start is present.
  3. 3
    Stop immediately before the stop index, if a stop is present.
  4. 4
    If a boundary is omitted, apply the corresponding beginning or end shortcut.
  5. 5
    For slice assignment, count the selected elements and compare that count with the replacement list.
  6. 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.

Related concepts

More concepts starting with I