Concepts / Understanding List Indexing

Understanding List Indexing

Slicing extracts a contiguous subset of a list using the syntax list[start:stop:step], where start is inclusive, stop is exclusive, and step is optional (default 1).

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

From One Position to Many

List indexing retrieves one element at a specific position. List slicing retrieves several elements by describing a range of positions. The general slice notation is list[start:stop:step]. The start position is included, the stop position is excluded, and the step controls how far the slice moves between selected positions.

Key point

The most important rule is that slicing includes start but excludes stop. A slice from index 1 to index 3 selects positions 1 and 2, not position 3.

Mapping Positions to Elements

To reason about a slice, first map each index to its element. Consider the six-element list t containing a, b, c, d, e, and f. Its positive indices begin at 0 and increase from left to right. The same elements also have negative indices counted from the right: -1 is the last element, -2 is the second-to-last, and so on.

a0 and -6b1 and -5c2 and -4d3 and -3e4 and -2f5 and -1
Which list element does each positive or negative index refer to?

Negative indices do not create a different sequence. They provide another way to name the same positions. For example, the final element can be reached with index 5 in this six-element list or with index -1. This makes negative indices useful when you want to work from the end without using the exact list length.

Tracing the Slice Boundary

A reliable way to predict a slice is to mark the starting position, move toward the stopping position, and leave the stop position out. For t[1:3], begin at index 1, include the element there, then include index 2. Stop before index 3. The result is b and c.

aindex 0bincludedbindex 1cincludedcindex 2dindex 3selectedselected
As the slice moves from start to stop, which positions are included and which position marks the excluded boundary?
Worked example

Reading t[1:3]

Determine which elements are selected from the six-element list t containing a, b, c, d, e, and f.

  1. 1
    Start at index 1

    Index 1 contains b, and the start boundary is inclusive, so b is selected.

  2. 2
    Move to index 2

    Index 2 contains c, so c is also selected.

  3. 3
    Reach index 3

    Index 3 contains d, but the stop boundary is exclusive, so d is not selected.

Result

The slice contains b and c.

Combining Start Stop and Step

The three positions in list[start:stop:step] have separate jobs. Start identifies the first position considered, stop identifies the boundary where selection ends, and step determines how many positions to move between selected elements. All three parameters are optional. When step is omitted, it defaults to 1, so each position in the selected range is visited.

aindex 0cindex 2eindex 4advance 2advance 2
Starting at the first selected position, which elements are visited next when the slice advances by a step of 2?

With a step of 2, the slice selects every other element. For the list t, t[::2] begins at the default starting boundary, advances through indices 0, 2, and 4, and returns a, c, and e. A step of 3 would select every third element. A negative step can be used to reverse the order of selected elements.

What do you think happens?

Which elements are selected by t[1:5:2] when t contains a, b, c, d, e, and f?

  • b and d

  • b, c, d, and e

  • c and e

  • a, c, and e

Reveal answer
Answer:

b and d

The slice starts at index 1, includes that position, advances by 2 to index 3, and stops before index 5. Indices 1 and 3 contain b and d.

Leaving Boundaries Empty

An omitted start tells Python to begin at index 0. An omitted stop tells Python to continue through the end of the list. Therefore, t[:4] selects the first four elements, while t[3:] selects every element from index 3 onward. Omitting both start and stop, as in t[:], spans the entire list.

t[:]entire listindex 0default startt[:4]indices 0 through 3end of listdefault stopt[3:]index 3 to endomitted startomitted stopomitted startomitted stop
When start or stop is omitted, what boundary does Python use, and what portion of the list is selected?

Reading from the End

Negative slice boundaries use the same inclusive-start and exclusive-stop rules as positive boundaries. In t[1:-1], the slice begins at index 1 and stops before the final element. This selects every element except the first and last: b, c, d, and e.

Worked example

Reading t[1:-1]

Determine the selected elements when t contains a, b, c, d, e, and f.

  1. 1
    Locate the start

    Index 1 contains b, and the start is included.

  2. 2
    Interpret the stop

    Index -1 refers to f, the final element. Because stop is exclusive, f is not included.

  3. 3
    Collect the range

    The selected positions are the elements from b through e.

Result

The slice contains b, c, d, and e.

Best practice

When using a negative boundary, translate it into an element before deciding what the slice returns. Remember that -1 names the final element, but a stop of -1 excludes that element.

A Complete Prediction Routine

Read slicestart:stop:stepSet startgiven or defaultSelected elementsnew listSet stopgiven or defaultAdvance by stepdefault 1interpretthenmoveuntil stop
How do start, stop, and step combine to determine the exact sequence of elements returned by a slice?

Use this routine for any slice: first identify the start, then identify the exclusive stop, and finally move by the step. If a boundary is missing, apply its default. If the step is missing, use 1. At each visited position, include the element unless that position is the stop boundary.

Worked example

Tracing t[::2]

Predict the result for the six-element list t containing a, b, c, d, e, and f.

  1. 1
    Apply the omitted start

    Because start is omitted, begin at index 0.

  2. 2
    Apply the omitted stop

    Because stop is omitted, continue toward the end of the list.

  3. 3
    Apply the step

    The step is 2, so visit indices 0, 2, and 4.

Result

The slice returns a, c, and e.

Mistakes Beginners Make

  • Common mistake

    Including the stop position

    What it looks like

    Treating t[1:3] as if it included index 3

    Why it fails

    The stop boundary is exclusive.

    Do this instead

    Select indices 1 and 2, so the result is b and c.

  • Common mistake

    Treating -1 as the first position

    What it looks like

    Reading -1 as the beginning of the list

    Why it fails

    Negative indices count from the end, and -1 refers to the last element.

    Do this instead

    Translate negative indices from the right before tracing the slice.

  • Common mistake

    Forgetting that step skips positions

    What it looks like

    Expecting t[::2] to contain every element

    Why it fails

    A step of 2 selects every other element.

    Do this instead

    Trace indices 0, 2, and 4 for t[::2].

  • Common mistake

    Assuming a slice changes the original list

    What it looks like

    Expecting a modified slice to modify the source list

    Why it fails

    Slicing creates a new list containing the selected elements.

    Do this instead

    Treat the sliced list as a separate list object.

Practice the Trace

MEDIUM

A list t contains a, b, c, d, e, and f. Predict the result of t[2:-1:2]. Explain which indices you visit, which boundary is excluded, and how the step changes the traversal.

Hints
  • Translate index 2 and negative index -1 into elements or positions.
  • Include the start position but stop before the position named by -1.
  • Begin at index 2 and advance by 2.
EASY

Describe what happens when the start and stop are both omitted but the step is 3. State the default boundaries and identify the pattern of selected positions.

Hints
  • An omitted start begins at index 0.
  • An omitted stop extends to the end.
  • A step of 3 selects every third position.

Slice Rules to Remember

  1. 1
    Slicing uses list[start:stop:step] to create a new list from selected elements.
  2. 2
    The start boundary is inclusive, while the stop boundary is exclusive.
  3. 3
    An omitted start means index 0, and an omitted stop means the end of the list.
  4. 4
    Negative indices count from the end, with -1 naming the final element.
  5. 5
    The step controls the positions visited; the default step is 1.

Key Takeaways

  • A slice selects multiple list elements using start, stop, and optional step parameters.
  • Always include the start position and exclude the stop position when tracing a slice.
  • Missing boundaries use the beginning or end of the list, while a missing step defaults to 1.
  • Negative indices name positions from the end of the list.
  • Slicing creates a new list and does not modify the original list.

Related concepts

More concepts starting with U