Question
How does Python's slice notation work? For example, when writing expressions such as a[x:y:z], a[:], or a[::2], how can you determine which elements are included in the result?
Examples to understand include:
a[x:y:z]
a[:]
a[::2]
The main goal is to understand how Python interprets the start, stop, and step parts of a slice, and which items from a sequence end up in the sliced result.
Short Answer
By the end of this page, you will understand how Python slicing selects elements from sequences like lists, strings, and tuples. You will learn what start, stop, and step mean, why the stop index is excluded, how negative indexes and negative steps work, and how to read common slice patterns such as a[:], a[1:4], and a[::-1].
Concept
Python slicing is a way to extract part of a sequence. A sequence can be a list, string, tuple, or other object that supports indexing.
The general form is:
sequence[start:stop:step]
Each part has a job:
start: where to beginstop: where to stop, but not including that positionstep: how far to move each time
You can leave out parts, and Python fills in sensible defaults.
For example:
nums = [10, 20, 30, 40, 50, 60]
print(nums[1:4])
Output:
[20, 30, 40]
This starts at index 1, moves forward by 1, and stops before index 4.
Slicing matters because it is used everywhere in Python:
Mental Model
Think of a slice like walking through numbered boxes on a shelf.
starttells you which box to begin atstoptells you the first box you are not allowed to takesteptells you how many boxes to jump each time
So in this slice:
a[1:6:2]
You are saying:
- Start at box
1 - Take an item
- Jump by
2 - Stop before box
6
If the boxes contain:
['a', 'b', 'c', 'd', 'e', 'f', 'g']
Then you take:
- index
1->'b' - index
3->'d'
Syntax and Examples
The full syntax is:
sequence[start:stop:step]
All three parts are optional:
sequence[:] # copy all items
sequence[start:] # from start to the end
sequence[:stop] # from the beginning to before stop
sequence[::step] # whole sequence, skipping by step
Basic examples
nums = [0, 1, 2, 3, 4, 5, 6]
print(nums[2:5]) # [2, 3, 4]
print(nums[:4]) # [0, 1, 2, 3]
print(nums[3:]) # [3, 4, 5, 6]
print(nums[:]) # [0, 1, 2, 3, 4, 5, 6]
Explanation:
nums[2:5]starts at index2and stops before5nums[:4]starts at the beginning and stops before
Step by Step Execution
Consider this example:
nums = [10, 20, 30, 40, 50, 60, 70]
result = nums[1:6:2]
print(result)
Output:
[20, 40, 60]
Now trace it step by step.
Step 1: Understand the indexes
# index: 0 1 2 3 4 5 6
nums = [10, 20, 30, 40, 50, 60, 70]
Step 2: Read the slice parts
nums[1:6:2]
start = 1stop = 6step = 2
Real World Use Cases
Slicing is useful in many everyday Python tasks.
Working with text
filename = "report.csv"
print(filename[:-4]) # report
This removes the file extension by taking everything except the last 4 characters.
Getting the first few records
users = ["Ana", "Ben", "Cara", "Dan", "Eva"]
print(users[:3])
Useful for previews, dashboards, or showing the top results.
Skipping items in data
values = [5, 10, 15, 20, 25, 30]
print(values[::2])
This can be used to sample every second reading from collected data.
Reversing data
recent = [1, 2, 3, 4, 5]
print(recent[::-])
Real Codebase Usage
In real projects, slicing often appears as a small but important tool.
Creating safe subsets of data
Developers often slice data before processing it:
batch = records[:100]
This is common in scripts, APIs, and data pipelines.
Copying lists
copied = original[:]
This creates a shallow copy of the list. It is often used when code should avoid modifying the original list.
Limiting output
top_results = search_results[:10]
Very common in web apps, pagination logic, and command-line tools.
Reversing for display
latest_logs = logs[::-1]
Useful when the newest entries should appear first.
Combining slicing with validation
A guard clause may check input length before slicing:
def get_prefix(code):
if len(code) < 3:
return code
return code[:]
Common Mistakes
1. Forgetting that stop is excluded
Broken expectation:
nums = [0, 1, 2, 3, 4]
print(nums[1:3]) # beginner may expect [1, 2, 3]
Actual result:
[1, 2]
How to avoid it:
- Remember: Python stops before
stop - Think of
stopas the first index not included
2. Using the wrong direction with a negative step
Broken code:
nums = [0, 1, 2, 3, 4]
print(nums[1:4:-1])
Result:
[]
Why:
Comparisons
Slicing patterns compared
| Pattern | Meaning | Example result |
|---|---|---|
a[i] | one item at index i | a[2] -> single value |
a[i:j] | items from i to before j | a[1:4] |
a[i:j:k] | items from i to before j, jumping by k | a[1:6:2] |
a[:] |
Cheat Sheet
sequence[start:stop:step]
Rules
startis includedstopis excludedstepcannot be0- omitted values use defaults
Common patterns
a[:] # all items
a[2:] # from index 2 to end
a[:5] # from start to before index 5
a[::2] # every second item
a[1::2] # every second item, starting at index 1
a[::-1] # reversed copy
Negative indexes
a[-1] # last item
a[-2] # second-last item
a[:-1] # all except last
a[-3:] # last three items
Direction rules
- positive step moves forward
FAQ
What does a[x:y:z] mean in Python?
It means: start at index x, stop before index y, and move by z each time.
Why is the stop index not included in a slice?
Python uses an exclusive stop so that slices fit together cleanly and match the behavior of range().
What does a[:] do?
It returns a slice of the whole sequence. For lists, this creates a shallow copy.
What does a[::2] mean?
It means take every second item from the entire sequence.
How do negative indexes work in slices?
Negative indexes count from the end. For example, -1 is the last item.
How do I reverse a list or string with slicing?
Use:
a[::-1]
This walks through the sequence backward.
Why does my slice return an empty list?
Usually the step direction does not match the start and stop positions. For example, a negative step moves backward, not forward.
Mini Project
Description
Build a small Python program that demonstrates common slicing patterns on a list of daily temperatures. This project helps you practice reading slices, selecting ranges, skipping items, and reversing data in a realistic context.
Goal
Create a script that extracts useful subsets from a temperature list using Python slicing.
Requirements
- Create a list of at least 7 temperature values.
- Print the first 3 temperatures.
- Print the last 3 temperatures.
- Print every second temperature.
- Print the temperatures in reverse order.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.
Convert Bytes to String in Python 3
Learn how to convert bytes to str in Python 3 using decode(), text mode, and proper encodings with practical examples.