Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does a Python slice stop before its ending index?

Quick answer

Python slices include the start index but exclude the stop index. So items[1:3] takes the items at indexes 1 and 2, not index 3. Think of the stop as the boundary just after the last item you want. To include the item at index 3, use items[1:4]; the stop can be the sequence length when you want to reach its end. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does items[1:3] give me two items when I expected it to include the item at index 3?

Geez

Because the slice’s stop index is a boundary, not the index of the last item to include. A slice includes its start and stops before its end index: items[1:3] includes indexes 1 and 2, but not 3. [S1]

That rule also makes the number of selected positions easy to see: subtract the start from the stop. Here, 3 - 1 is 2, so the slice covers two positions. The key habit is to read the second number as “stop before here.”

Follow-up question

Can you show me how the boundary lines up with the list positions?

Geez

Take this list, with each item’s index shown above it:

items = ['a', 'b', 'c', 'd']
# indexes:  0    1    2    3

The slice items[1:3] starts at index 1 and stops before index 3. It returns ['b', 'c'], leaving 'd' out. [S1] You can also picture the boundaries between items:

boundary:  0    1    2    3    4
items:          a    b    c    d
indexes:        0    1    2    3

Indexes identify items; the stop marks where taking items ends. In this example, boundary 3 is just before the item at index 3.

Follow-up question

What should I write if I want to include the item at index 3?

Geez

Use the next boundary as the stop: items[1:4]. It includes indexes 1, 2, and 3, giving ['b', 'c', 'd']. [S1]

A useful translation is: “include through index 3” means “stop at boundary 4.” Keep the start the same and move the stop one position past the final item you want. If you want the slice to reach the end of the list, the stop can equal the sequence length. [S1]

Follow-up question

How can I check a slice when I keep getting one fewer item than expected?

Geez

Write down the item indexes and the two boundaries, then trace the slice before changing it. If you want 'b' through 'd' from the four-item list, use:

items[1:4]  # ['b', 'c', 'd']

Check it in this order:

  1. Start at index 1, so 'b' is included.
  2. Continue up to stop boundary 4.
  3. Stop before that boundary, including index 3 but not index 4.

If you wrote items[1:3], the slice stops before index 3, so it cannot include 'd'. Set the stop one past the last index you want. For any slice, compare the start, last included index, and stop boundary with the items you intended to select.

A practical checklist

  • Write the list items beside their indexes.

  • Mark the start: that item is included.

  • Treat the stop as a boundary; its index is excluded.

  • To include a desired final index, set the stop one higher.

  • Trace the selected indexes and compare them with the items you intended.

A few more useful answers

Does the stop index refer to an item or a position?

Treat it as the position before which the slice stops. The item at the stop index is not included. [S1]

Can I use the list length as the stop?

Yes. A slice’s end boundary can equal the sequence length, letting the slice reach the end. [S1]

What is the quickest way to remember the rule?

Think “start here, stop before there.” To include the item at index 3, use stop boundary 4. [S1]

Sources

References used for this guide.

  1. [S1] An Informal Introduction to Python — sequence slicing
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work