List Slicing
Slicing extracts parts of a list using [start:stop:step]. It's one of Python's most powerful features.
15 min•By Priygop Team•Updated 2026
Slicing
Slicing
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Basic slicing [start:stop] (stop is excluded!)
print(nums[2:5]) # [2, 3, 4]
print(nums[:3]) # [0, 1, 2] (from start)
print(nums[7:]) # [7, 8, 9] (to end)
print(nums[:]) # full copy
# Negative indexing
print(nums[-3:]) # [7, 8, 9]
print(nums[:-2]) # [0, 1, 2, 3, 4, 5, 6, 7]
# Step
print(nums[::2]) # [0, 2, 4, 6, 8] (every 2nd)
print(nums[1::2]) # [1, 3, 5, 7, 9] (odd indices)
print(nums[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (reversed!)
# Slice assignment
nums[2:5] = [20, 30, 40]
print(nums) # [0, 1, 20, 30, 40, 5, 6, 7, 8, 9]
# String slicing works the same way
text = "Hello, World!"
print(text[7:12]) # World
print(text[::-1]) # !dlroW ,olleHTip
Tip
[::-1] reverses any sequence — lists, strings, tuples. It's the most Pythonic way to reverse without modifying the original.
Diagram
Loading diagram…
Slicing creates new list (shallow copy). Negative step reverses. [start:stop:step] — all optional.
Common Mistake
Warning
Slicing creates a new list. nums[2:5] doesn't modify the original. But slice assignment nums[2:5] = [20,30,40] does modify in place.
Practice Task
Note
(1) Get every other element. (2) Reverse a string using slicing. (3) Extract the last 3 items. (4) Replace a slice with new values.