python / beginner
Snippet
List Slicing Basics
Slicing allows you to create a new list by extracting a specific range of elements from an existing list using the colon operator.
snippet.py
1
2
3
numbers = [10, 20, 30, 40, 50]sub_list = numbers[1:4]print(sub_list)
Breakdown
1
numbers = [10, 20, 30, 40, 50]
Creates a list containing five integers.
2
sub_list = numbers[1:4]
Extracts elements from index 1 up to (but not including) index 4.
3
print(sub_list)
Outputs the result: [20, 30, 40].