Slices in Python are a very useful tool that allows us to extract portions of sequences of elements (like lists, tuples, or strings) with very little code.
Slices allow us to work with specific fragments of a sequence, without having to modify or iterate over the entire sequence.
The basic syntax for creating a slice in Python is as follows:
sequence[start:end:step]
start: Index where the slice begins (included)end: Index where the slice ends (not included)step: Step size or increment between slice elements (optional)
Special Considerations
Modifying Values with Slices
Slices can also be used to modify values in a sequence.
fruits = ["apple", "banana", "cherry", "date", "grape"]
fruits[1:3] = ["pear", "orange"] # Replaces "banana" and "cherry" with "pear" and "orange"
In this case,
fruits[1:3]selects the elements"banana"and"cherry"- Replaces them with
"pear"and"orange".
Practical Examples
Let’s assume we have a list of numbers:
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Now, let’s see how Slicing behaves with different combinations of start, stop, and step:
Not Specifying Start, Stop, or Step
result = numbers[:]
print(result) # Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In this case, the slice copies the entire original list, since no value is specified for start, end, or step.
Specifying Start and Stop
result = numbers[2:7]
print(result) # Result: [2, 3, 4, 5, 6]
Here, the slice begins at index 2 and ends at index 6 (non-inclusive), using the default step of 1.
Specifying Only Stop
result = numbers[:5]
print(result) # Result: [0, 1, 2, 3, 4]
In this case, the Slice starts from the first element and ends at index 5 (non-inclusive), using the default step of 1.
Specifying Only Start
result = numbers[3:]
print(result) # Result: [3, 4, 5, 6, 7, 8, 9]
Here, the Slice begins at index 3 and continues to the last element of the list, using the default step of 1.
Specifying Only Step
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
even_numbers = numbers[::2] # Result: [0, 2, 4, 6, 8]
Here, numbers[::2] creates a new slice of the numbers list with a step of 2, meaning it extracts every second element, resulting in [0, 2, 4, 6, 8].
Specifying Everything
result = numbers[1:8:2]
print(result) # Result: [1, 3, 5, 7]
In this example, the slice begins at index 1, ends at index 8 (non-inclusive), and uses a step of 2, taking every second element from the start to the specified end.
Negative Indices at the Start
Using negative indices at the start of the slice extracts elements from the end of the list.
last_three = numbers[-3:] # Extracts the last three elements: [7, 8, 9]
In this case, numbers[-3:] creates a slice that includes the last three elements of the numbers list.
Negative Indices at the End
It’s also possible to use negative indices at the end of the slice to define the start from the end of the list.
without_last_three = numbers[:-3] # Extracts all elements except the last three: [0, 1, 2, 3, 4, 5, 6]
Here, numbers[:-3] creates a slice that includes all elements of the numbers list except the last three.
Negative Step
Using a negative step in the slice reverses the order of the extracted elements.
reversed = numbers[::-1] # Reverses the order of the list: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
By specifying numbers[::-1], a slice is created that starts from the last element to the first, thus reversing the order of the original list.
