capypad
0 day streak
python / beginner
Snippet

List Slicing

Slicing allows you to extract a specific portion of a list using start and end indices.

snippet.py
python
1
2
3
colors = ["red", "green", "blue", "yellow"]
subset = colors[1:3]
print(subset)
Breakdown
1
colors = [...]
Creates a list with four string elements.
2
subset = colors[1:3]
Gets elements from index 1 up to (but not including) index 3.
3
print(subset)
Outputs ['green', 'blue'] to the console.