Python Basics — List Slicing
List Slicing
List slicing works exactly like string slicing — but since lists are mutable, you can also use slicing to modify, insert, and delete elements.
Basic Slicing
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[2:5]) # [2, 3, 4]
print(numbers[:3]) # [0, 1, 2]
print(numbers[7:]) # [7, 8, 9]
print(numbers[-3:]) # [7, 8, 9]
print(numbers[::2]) # [0, 2, 4, 6, 8]
print(numbers[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Shallow Copy via Slicing
original = [1, 2, 3, 4, 5]
copy = original[:]
copy.append(6)
print(original) # [1, 2, 3, 4, 5] (unchanged)
print(copy) # [1, 2, 3, 4, 5, 6]
Assigning to Slices
You can replace a section of a list with new elements:
numbers = [0, 1, 2, 3, 4, 5]
# Replace elements 2-4
numbers[2:5] = [20, 30, 40]
print(numbers) # [0, 1, 20, 30, 40, 5]
Insert Elements via Slice
numbers = [0, 1, 4, 5]
# Insert at position 2 (empty slice)
numbers[2:2] = [2, 3]
print(numbers) # [0, 1, 2, 3, 4, 5]
Replace with Different Length
numbers = [0, 1, 2, 3, 4, 5]
# Replace 3 elements with 1
numbers[1:4] = [99]
print(numbers) # [0, 99, 4, 5]
numbers = [0, 1, 2, 3, 4, 5]
# Replace 1 element with 3
numbers[2:3] = [20, 21, 22]
print(numbers) # [0, 1, 20, 21, 22, 3, 4, 5]
Deleting via Slices
numbers = [0, 1, 2, 3, 4, 5]
# Delete elements 2-4
del numbers[2:5]
print(numbers) # [0, 1, 5]
numbers = [0, 1, 2, 3, 4, 5]
# Delete by assigning empty list
numbers[2:5] = []
print(numbers) # [0, 1, 5]
Slice vs Copy Behavior
When you slice a list, you get a shallow copy:
# Simple values — fully independent
original = [1, 2, 3]
copy = original[:]
copy[0] = 99
print(original) # [1, 2, 3] (unchanged)
# Nested objects — inner objects are shared!
original = [[1, 2], [3, 4]]
copy = original[:]
copy[0][0] = 99
print(original) # [[99, 2], [3, 4]] (inner list changed!)
For truly independent copies of nested lists, use copy.deepcopy():
import copy
original = [[1, 2], [3, 4]]
deep = copy.deepcopy(original)
deep[0][0] = 99
print(original) # [[1, 2], [3, 4]] (unchanged)
Step Slicing with Lists
numbers = list(range(10))
# Every other element
print(numbers[::2]) # [0, 2, 4, 6, 8]
# Every third element
print(numbers[::3]) # [0, 3, 6, 9]
# Reverse
print(numbers[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
# Reverse every other
print(numbers[::-2]) # [9, 7, 5, 3, 1]
Practical Examples
Splitting a List into Chunks
def chunk(lst, size):
return [lst[i:i + size] for i in range(0, len(lst), size)]
data = list(range(1, 11))
print(chunk(data, 3))
# [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
Rotating a List
def rotate(lst, n):
n = n % len(lst)
return lst[n:] + lst[:n]
items = [1, 2, 3, 4, 5]
print(rotate(items, 2)) # [3, 4, 5, 1, 2]
print(rotate(items, -1)) # [5, 1, 2, 3, 4]
Summary
- List slicing:
list[start:stop:step] - Slicing returns a new (shallow copy) list
- You can assign to slices to replace, insert, or delete elements
del list[start:stop]removes elements- Shallow copies share nested objects — use
copy.deepcopy()for full independence list[:]creates a shallow copy of the entire list
Next up: List Comprehensions →