Lesson 4 of 16 · Pythonic Code
Slicing in Depth
Master Python slicing for lists and strings: start, stop and step, negative indexes, reversing, copying and the slice patterns used in data work.
- 8 min read
- Intermediate
- Reviewed
- How start, stop and step work in a slice
- How negative indexes count from the end
- How to reverse and copy a list with a slice
- How to replace part of a list
A quick reminder
A slice takes part of a list or string. You write items[start:stop]. The slice starts at start and stops before stop.
letters = ["a", "b", "c", "d", "e", "f"] print(letters[1:4]) print(letters[:3]) print(letters[3:])
letters[1:4]gives positions 1, 2 and 3.- Leave out
startto begin at the start. - Leave out
stopto go to the end.
A useful way to think about it: items[:n] is "the first n items" and items[n:] is "everything after the first n".
Negative indexes
A negative index counts from the end. -1 is the last item, -2 the one before it.
letters = ["a", "b", "c", "d", "e", "f"] print(letters[-1]) print(letters[-3:]) print(letters[:-2])
letters[-3:] means "the last three". letters[:-2] means "everything except the last two".
The step
A third number, the step, says how far to jump each time.
numbers = list(range(10)) print(numbers) print(numbers[::2]) print(numbers[1::2]) print(numbers[1:8:3])
numbers[::2] takes every second item, starting with the first. numbers[1::2] takes every second item starting with the second.
Reversing
A step of -1 walks backwards. With no start or stop, it reverses the whole sequence.
numbers = [1, 2, 3, 4, 5] word = "level" print(numbers[::-1]) print(word[::-1]) print(word == word[::-1])
The last line checks whether a word is a palindrome: it reads the same both ways.
Slices never fail
Asking for a single position that does not exist raises an IndexError. A slice is more forgiving: it simply gives you what is there.
letters = ["a", "b", "c"] print(letters[1:100]) print(letters[5:])
Slicing makes a copy
A slice is a new list. Changing it does not change the original. items[:] is a quick way to copy a whole list.
original = [1, 2, 3] copy = original[:] copy.append(4) alias = original alias.append(99) print(original) print(copy)
alias = original does not copy anything: both names point to the same list, so the 99 appears in original too. The slice made a real copy, so the 4 did not.
Replacing part of a list
You can assign to a slice. The new items replace the old ones, and the list can grow or shrink.
steps = ["plan", "draft", "draft", "draft", "publish"] steps[1:4] = ["write", "review"] print(steps)
Slicing text
Strings slice exactly like lists. This is handy for trimming known prefixes or shortening long text.
filename = "report_2026.csv" summary = "Python is the most popular language for building AI systems today." print(filename[:-4]) print(filename[-3:]) print(summary[:30] + "...")
Common mistakes
Use three slices to show the end of the data, every second value and the whole list reversed.
readings = [12, 15, 11, 18, 21, 19, 25, 23, 30, 28] # Print three slices, one per line: # 1. the last three readings # 2. every second reading, starting with the first # 3. all readings in reverse order # Output must be exactly: # [23, 30, 28] # [12, 11, 21, 25, 30] # [28, 30, 23, 25, 19, 21, 18, 11, 15, 12]
What does 'Python'[1:4] give?
Summary
items[start:stop:step]takes part of a list or string. The stop is not included.- Negative indexes count from the end:
items[-3:]is the last three. items[::-1]reverses, anditems[:]copies.- Assigning to a slice replaces that part of a list.
You have finished the Pythonic Code module. Next, you will look at functions in more depth.
Finished reading? Tick it off to track your progress.