The Python Journey

Indexing and Slicing

Indexing and Slicing

Page Contents

Overview

Working with sequences — list, tuple, and str — is something we do in almost every Advent of Code puzzle. Python’s indexing and slicing syntax lets you pluck out single items, grab ranges from the front or back, and reverse sequences in a single readable expression without writing loops.

Indexing (Positive and Negative)

Python sequences are zero-indexed from the left and negative-indexed from the right:

  • 0 is the first element, 1 is the second, and so on.
  • -1 is the last element, -2 is the second-to-last, and -3 is the third-to-last.

Take a 6-element list of IDs: ids = [57, 43, 76, 78, 45, 55].

Element 57 43 76 78 45 55
Positive index 0 1 2 3 4 5
Negative index -6 -5 -4 -3 -2 -1
ids = [57, 43, 76, 78, 45, 55]

print(ids[0])   # 57 (first item)
print(ids[2])   # 76 (third item)
print(ids[-1])  # 55 (last item)
print(ids[-3])  # 78 (third from the end)

Note that indexing a position that does not exist (e.g. ids[10] or ids[-10]) raises an IndexError.

Basic Slicing (seq[start:stop])

While indexing fetches a single element, slicing returns a new sequence containing a range of elements using seq[start:stop].

The golden rule of Python slicing is that ranges are half-open:

  • start is inclusive (included in the result).
  • stop is exclusive (we stop before this index).
ids = [57, 43, 76, 78, 45, 55]

print(ids[1:4])  # [43, 76, 78]  (indices 1, 2, 3 — stops before 4)
print(ids[:3])   # [57, 43, 76]  (omit start -> defaults to 0, first 3 items)
print(ids[3:])   # [78, 45, 55]  (omit stop -> goes all the way to the end)
print(ids[:])    # [57, 43, 76, 78, 45, 55] (shallow copy of the full list)

One easy mental trick: len(seq[a:b]) is always b - a (when both are within bounds), and seq[:k] + seq[k:] always reconstructs the original sequence without duplicating or skipping index k.

Slicing from the End (seq[-n:])

Combining negative indices with slicing is particularly useful when you want the tail of a list or string:

  • seq[-3:] starts 3 positions from the end and slices through to the end — giving you the last 3 elements.
  • seq[:-1] starts at 0 and stops before the last element — handy for dropping a trailing character or newline.
  • seq[-4:-1] grabs elements -4, -3, and -2 (stopping before -1).
ids = [57, 43, 76, 78, 45, 55]

print(ids[-3:])    # [78, 45, 55] (last 3 items)
print(ids[:-1])    # [57, 43, 76, 78, 45] (everything except the last item)
print(ids[-4:-1])  # [76, 78, 45]

Unlike single-element indexing, slicing never raises IndexError if your bounds go past the ends of the sequence — Python quietly clamps them to what is available:

short = [57, 43]
print(short[-5:])  # [57, 43] (clamps safely to the start)
print(short[:10])  # [57, 43] (clamps safely to the end)

Steps and Reversing (seq[start:stop:step])

Slices accept an optional third value — step — which controls how many positions to jump between items:

ids = [57, 43, 76, 78, 45, 55]

print(ids[::2])    # [57, 76, 45] (every second item, starting at 0)
print(ids[1::2])   # [43, 78, 55] (every second item, starting at 1)
print(ids[::-1])   # [55, 45, 78, 76, 43, 57] (full list in reverse order)

You can also combine negative steps with slicing from the end. For example, if you want the last 3 elements with the very last item first, ids[::-1][:3] (or ids[-1:-4:-1]) returns [55, 45, 78].

Advent of Code Examples

  • 2026 Day 1 (Dusty Shed): id_counts.most_common() returns all (item_id, count) pairs sorted from most frequent to least frequent. Slicing [-3:] grabs the three rarest IDs straight off the end.
  • Splitting strings and grid rows: Parsing fixed-width puzzle lines like line[:mid] and line[mid:] (e.g. halving rucksack compartments), or trimming brackets with line[1:-1].