# Python: slicing

A slice cuts a piece out of a sequence by the rule "from which index, up to which, with what step". The notation is compact, but it has two places where almost everyone slips: the right bound is not included, and with a negative step the bounds swap places. Let us go through both.

## How to read the notation

The full form is `sequence[start:stop:step]`. The start is included, the stop is
not. Any part can be omitted.

```python
a = [0, 1, 2, 3, 4, 5]
print(a[1:4])    # [1, 2, 3]
print(a[:3])     # [0, 1, 2]
print(a[3:])     # [3, 4, 5]
print(a[:])      # [0, 1, 2, 3, 4, 5]
print(a[::2])    # [0, 2, 4]
```

The "stop is excluded" rule feels awkward until you notice two consequences: the
length of a slice equals `stop - start`, and `a[:i]` and `a[i:]` together give the
original list with no overlap and no gaps.

```python
i = 2
print(a[:i] + a[i:] == a)    # True
print(len(a[1:4]))           # 3
```

## Negative indexes and a negative step

A negative index counts from the end: `-1` is the last item, `-2` the one before
it.

```python
print(a[-1])     # 5
print(a[-2:])    # [4, 5]   the last two
print(a[:-2])    # [0, 1, 2, 3]   everything except the last two
```

A negative step goes from right to left, and then the start must be to the right
of the stop:

```python
print(a[::-1])    # [5, 4, 3, 2, 1, 0]
print(a[4:1:-1])  # [4, 3, 2]
print(a[1:4:-1])  # []  — empty: we go left, but the stop is to the right
```

`a[::-1]` is the idiomatic way to reverse. Lists also have a `reverse()` method
that changes the list in place; a slice returns a new one.

## Slicing strings

A string is a sequence too, so the rules are the same, but the result is a string,
not a list of characters.

```python
s = "programming"
print(s[:7])        # program
print(s[-5:])       # mming
print(s[::-1])      # gnimmargorp
print(s[::3])       # pgmn
print(s[4:4])       # an empty string
```

Slicing is handy for cutting fixed-format records: `date[:4]` is the year in
`"2026-09-21"`, `date[5:7]` is the month. For variable-length formats `split` is
more reliable — see the article on [Python strings](https://skillok.org/topics/python-strings).

## A slice as a copy, and assigning to a slice

`a[:]` creates a new list with the same items — a shallow copy:

```python
b = a[:]
b.append(99)
print(len(a), len(b))    # 6 7
```

Nested objects stay shared, just as with `a.copy()`. More on that in the article
on [Python lists](https://skillok.org/topics/python-lists).

You can assign to a slice, and the length of the piece does not have to stay the
same:

```python
nums = [0, 1, 2, 3, 4]
nums[1:3] = ["a", "b", "c"]
print(nums)          # [0, 'a', 'b', 'c', 3, 4]
nums[0:2] = []       # deleting a piece
print(nums)          # ['b', 'c', 3, 4]
del nums[::2]
print(nums)          # ['c', 4]
```

You cannot do this with strings: they are immutable, and `s[0:2] = "ab"` raises
`TypeError`.

## Why the result is empty

A slice never raises `IndexError` — beyond the bounds it simply returns whatever
is there, down to nothing. That is convenient, but it hides bugs:

```python
print(a[10:20])   # []   no such indexes — empty, no exception
print(a[10])      # IndexError: list index out of range
print(a[3:1])     # []   start to the right of stop with step +1
print(a[::0])     # ValueError: slice step cannot be zero
```

If a slice is unexpectedly empty, check in order: are the bounds swapped, do you
need a negative step, did you count the stop as inclusive? A quick self-check is
to print `len(result)` next to the number you expected.

Practice: take a list of twenty numbers and, in one line, get every third item
counting from the end; then split the list into chunks of five with
`[a[i:i + 5] for i in range(0, len(a), 5)]` — you will need this idiom often.

## Step-by-step plan

1. **Three simple slices** — On a six-item list, get the first three, the last two and a copy of the whole list.
2. **Step and reverse** — Print every second item and reverse the sequence with [::-1].
3. **Negative bounds** — Get "everything except the last two" and "the last two" and compare with the original length.
4. **Assign to a slice** — Replace two items with three and delete a piece with nums[i:j] = [].
5. **Split into chunks** — Cut a list of twenty numbers into chunks of five with a list comprehension and a slice.

## Check yourself

1. What is len([0, 1, 2, 3, 4, 5][1:4])?
2. What does print("python"[::-1]) print?
   - python
   - nohtyp
   - noht
   - nothing
3. What is len([0, 1, 2, 3, 4, 5][3:1])?
4. What does print([0, 1, 2, 3, 4][1:3]) print?
   - [1, 2]
   - [1, 2, 3]
   - [0, 1, 2]
   - [2, 3]

## Sources

- [Sequences and slicing in the docs](https://docs.python.org/3/library/stdtypes.html) — The table of sequence operations and the rules for bounds
- [Python tutorial: lists and strings](https://docs.python.org/3/tutorial/introduction.html) — A first look at slicing strings and lists
- [Glossary: slice](https://docs.python.org/3/glossary.html) — The exact definition of a slice object

---

https://skillok.org/articles/en/python-slicing
