sethserver.com Subscribe
Grid diagram with colorful geometric shapes, scissors, and axis labels symbolizing data analysis, machine learning model optimization, and feature selection

Python Slice Notation: A Complete Guide to start:stop:step Syntax

By Seth Black • Updated: September 17, 2026

Python · 3 min read

Like this kind of writing? Get one email a week with notes on startups, AI, and the occasional strong opinion about Python: subscribe to the newsletter.

I had to re-learn Python slice notation an embarrassing number of times.

Not because it's hard. My brain just refused to store "stop is exclusive" next to "don't touch a hot pan." I'd write a[0:3], get three items back, and feel mildly betrayed every time.

Then I started doing more LLM work, back when ChatGPT 3.5 had a tiny context window and you were constantly shaving tokens down to fit. Slices stopped being "that syntax I vaguely remember" and became something I reached for all day. You don't have time to argue with start:stop:step when your prompt is already too long.

Slice notation is three numbers

The whole deal is:

a[start:stop:step]

Here's the list we'll mess with:

menu = ['eggs', 'sausage', 'bacon', 'tomato', 'spam']
## index:   0        1          2        3        4

Stop is exclusive (yes, again)

This is the rule that feels wrong for about a week, then quietly makes your code cleaner forever.

menu[0:3]  # ['eggs', 'sausage', 'bacon']

It returns indices 0, 1, 2. Not 3.

The mental model that finally stuck for me: stop is a boundary line, not "the last thing I want."

Also, with step=1, you get a handy shortcut:

So:

menu[2:5]  # 5 - 2 = 3 items -> ['bacon', 'tomato', 'spam']

This "exclusive stop" rule is why chunking works cleanly. You don't end up with overlaps or gaps because you got cute with -1 somewhere.

Blank start/stop is normal

Leave start empty and you start at the beginning. Leave stop empty and you go to the end.

menu[:2]   # ['eggs', 'sausage']
menu[2:]   # ['bacon', 'tomato', 'spam']

Negative indices work too:

menu[:-1]  # everything but last -> ['eggs', 'sausage', 'bacon', 'tomato']
menu[-2:]  # last two -> ['tomato', 'spam']
menu[1:-1] # middle -> ['sausage', 'bacon', 'tomato']

This shows up in real code constantly, like grabbing the last 500 log lines:

last_lines = logs[-500:]

If logs has 12 lines, you still get 12. Slicing is pretty forgiving. It won't throw a tantrum because you asked for more than you have.

Step: powerful, sometimes a readability crime

The third slot is step.

menu[::2]  # ['eggs', 'bacon', 'spam']

That means "start at the beginning, go to the end, take every 2nd item."

A pattern that comes up a lot is splitting a flat key/value list:

flat = ["a", 1, "b", 2, "c", 3]
keys = flat[::2]    # ['a', 'b', 'c']
values = flat[1::2] # [1, 2, 3]

Steps create a new list, though. If you're doing this on something big and you only need to iterate, a plain loop avoids allocating a copy:

for i in range(0, len(menu), 2):
    x = menu[i]

Not pretty, but it's clear what it's doing.

Reversing: the one slice everyone memorizes

menu[::-1]  # ['spam', 'tomato', 'bacon', 'sausage', 'eggs']

That returns a new reversed list.

If you want in-place:

menu.reverse()

If you want iteration without copying:

for x in reversed(menu):
    ...

Why slices show up in LLM work constantly

Token windowing is basically slicing, with a fancier name.

window = 256
chunk = tokens[i:i+window]

Exclusive stop means you get exactly window tokens without doing i+window-1 math and then staring at it like it might bite you.

A simple chunker:

def chunks(tokens, window):
    for i in range(0, len(tokens), window):
        yield tokens[i:i+window]

Sliding windows with overlap (common for embeddings):

def sliding_windows(tokens, window, stride):
    for i in range(0, max(0, len(tokens) - window + 1), stride):
        yield tokens[i:i+window]

That's the whole trick. Slices keep the math clean.

The five slice patterns I actually use

If you memorize anything, memorize these:

Everything else is you trying to be clever, and future-you will have to read it.

If you want the official docs, they're here and they're surprisingly readable:

https://docs.python.org/3/library/stdtypes.html#common-sequence-operations

-Sethers

Share this post
Newsletter

One email, once a week.

Notes on databases, systems, and the occasional strong opinion about Python. No spam, unsubscribe anytime.

Seth Black
Written by

Seth Black

Engineer and founder based in Texas. Writes about databases, AI, and running things in production. Embeds in small teams as lead engineer.

More from Python

View all →
Python

OpenAI Bought Astral - and my fav tool uv

Mar 23, 2026
Python

Python Pydantic Validation: Stop Writing Manual Checks

Mar 04, 2026
Python

Python asyncio Recipes

Mar 04, 2026