Stop 05 · Built-in Data StructuresTopics · 5/9

Slicing

Part of the Python Roadmap

In short

Want the first five rows, the last three characters, every second item or a reversed copy? Slicing does all of it with one compact bracket syntax that works on lists, tuples and strings.

Slicing with [start:stop:step] pulls out part of a list, tuple or string. The stop index is excluded, missing values mean 'from the start' or 'to the end', and [::-1] reverses a sequence.

The jaadu carefully cuts a long loaf of bread between two toothpick markers, lifting out the middle slices on a knife.

Think of it like this

Cutting slices from a loaf of bread

Mark where you start cutting and where you stop, and you get the slices in between. The stop mark is where the knife lands, so that slice isn't included. You could also take every second slice, or start from the end of the loaf. That's [start:stop:step].

Watch it happen

Slicing a list of seven days

Mon
0
Tue
1
start
Wed
2
Thu
3
Fri
4
stop
Sat
5
Sun
6

1/6days[1:4] starts at index 1 and stops before index 4.

The big ideas

What you need to know

Stop is excluded

s[2:5] gives indexes 2, 3 and 4. The length of the slice is stop minus start.

Blanks mean the edge

A missing start means 0, a missing stop means the end. s[:] is a full shallow copy.

Negatives count from the end

-1 is the last item. s[-2:] gives the last two, and s[:-1] drops the last one.

Slices never crash

s[2:100] simply stops at the end. Plain indexing like s[100] raises IndexError, but slicing is forgiving.

See it in code

Walk through the code

python
1phone = "9876543210"2print(phone[:5])        # 987653print(phone[-4:])       # 32104print("XXXXXX" + phone[-4:])   # mask all but last 45 6scores = [45, 67, 89, 12, 90, 33, 71]7print(scores[2:5])      # [89, 12, 90]8print(scores[::2])      # [45, 89, 90, 71]9print(scores[::-1])     # reversed copy10 11word = "malayalam"12print(word == word[::-1])   # True, it's a palindrome13 14backup = scores[:]      # a new list, not the same one15backup[0] = 016print(scores[0])        # still 45

1/4Strings slice too. [-4:] gives the last four digits, perfect for masking.

Masking a phone number, picking parts of a list, checking a palindrome and copying with [:].

Save yourself the pain

Common mistakes

Oops

Expecting nums[1:3] to include nums[3].

Do this instead

The stop index is never included. To get indexes 1 to 3, write nums[1:4].

Oops

Thinking s[::-1] reverses the original list.

Do this instead

Slicing always returns a new object. Use s.reverse() to reverse a list in place.

Your mission

PAN and date slicer

~15 min

Quick check

What is 'PYTHONIC'[2:6]?

Go deeper

  1. 1Get the first three and last three items of a list with slices.
  2. 2Reverse a string with [::-1] and check if a word is a palindrome.
  3. 3Use a step of 2 to take every other item.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge