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.

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.

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

Reading t[1:3]

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

Start at index 1: Index 1 contains b, and the start boundary is inclusive, so b is selected.

Move to index 2: Index 2 contains c, so c is also selected.

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

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.

advance 2advance 2aindex 0cindex 2eindex 4
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.

omitted startomitted stopomitted startomitted stopt[:]entire listindex 0default startt[:4]indices 0 through 3end of listdefault stopt[3:]index 3 to end
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.

Reading t[1:-1]

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

Locate the start: Index 1 contains b, and the start is included.

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

Collect the range: The selected positions are the elements from b through e.

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

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

interpretthenmoveuntil stopRead slicestart:stop:stepSet startgiven or defaultSelected elementsnew listSet stopgiven or defaultAdvance by stepdefault 1
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.

Tracing t[::2]

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

Apply the omitted start: Because start is omitted, begin at index 0.

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

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

The slice returns a, c, and e.

Mistakes Beginners Make

  • Including the stop position

    The stop boundary is exclusive.

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

  • Treating -1 as the first position

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

    Fix: Translate negative indices from the right before tracing the slice.

  • Forgetting that step skips positions

    A step of 2 selects every other element.

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

  • Assuming a slice changes the original list

    Slicing creates a new list containing the selected elements.

    Fix: 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. Slicing uses list[start:stop:step] to create a new list from selected elements.
  2. The start boundary is inclusive, while the stop boundary is exclusive.
  3. An omitted start means index 0, and an omitted stop means the end of the list.
  4. Negative indices count from the end, with -1 naming the final element.
  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.