python / beginner
Snippet
Extracting Parts of a List (Slicing)
Slicing allows you to create a new list by extracting a range of elements from an existing list using a start and end index.
snippet.py
1
2
3
items = [10, 20, 30, 40, 50]part = items[1:4]print(part)
Breakdown
1
items = [10, 20, 30, 40, 50]
Create a list with five integer elements.
2
part = items[1:4]
Extract elements from index 1 up to (but not including) index 4.
3
print(part)
Output the new list [20, 30, 40].