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]
- start: where you begin (0-based index)
- stop: where you end (exclusive)
- step: how far you jump each time (default
1)
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:
a[start:stop]returns aboutstop - startitems (assuming the list is long enough)
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:
- First N:
items[:N] - Last N:
items[-N:] - All but last:
items[:-1] - Every other:
items[::2] - Reverse copy:
items[::-1]
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