---
title: "Python Slice Notation: A Complete Guide to start:stop:step Syntax"
description: "Learn Python slice notation once and for all: start:stop:step explained with real examples from LLM token windowing, chunking, and everyday data manipulation. Includes the five essential slice patterns every Python developer should memorize."
image: "https://cdn.sethserver.com/blog-images/understanding-slice-notation-social-1767025492.webp"
---

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

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:

```python
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:

```python
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.

```python
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 about `stop - start` items (assuming the list is long enough)

So:

```python
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.

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

Negative indices work too:

```python
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](https://www.sethserver.com/python/python-logging-done-right.html):

```python
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`.

```python
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:

```python
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:

```python
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

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

That returns a **new** reversed list.

If you want in-place:

```python
menu.reverse()
```

If you want iteration without copying:

```python
for x in reversed(menu):
    ...
```

## Why slices show up in LLM work constantly

Token windowing is basically slicing, with a fancier name.

```python
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:

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

Sliding windows with overlap (common for embeddings):

```python
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


```json
{"@context": "https://schema.org", "@type": "BlogPosting", "headline": "Python Slice Notation: A Complete Guide to start:stop:step Syntax", "datePublished": "2020-12-01T00:00:00", "dateModified": "2026-09-17T19:47:38", "image": {"@type": "ImageObject", "url": "https://cdn.sethserver.com/blog-images/understanding-slice-notation-social-1767025492.webp", "width": 400, "height": 400}, "url": "https://www.sethserver.com/python/understanding-slice-notation.html", "author": {"@type": "Person", "name": "Seth Black", "url": "https://www.linkedin.com/in/seth-black-tx"}, "publisher": {"@type": "Organization", "name": "Sethserver", "logo": {"@type": "ImageObject", "url": "https://www.sethserver.com/static/images/PaperAirplane512.png"}}, "description": "Learn Python slice notation once and for all: start:stop:step explained with real examples from LLM token windowing, chunking, and everyday data manipulation. Includes the five essential slice patterns every Python developer should memorize.", "articleSection": "Technology"}
{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 1, "name": "sethserver", "item": "https://www.sethserver.com/"}, {"@type": "ListItem", "position": 2, "name": "Python", "item": "https://www.sethserver.com/python/"}, {"@type": "ListItem", "position": 3, "name": "Python Slice Notation: A Complete Guide to start:stop:step Syntax", "item": "https://www.sethserver.com/python/understanding-slice-notation.html"}]}
```
