The “slices” in Python are a very useful tool that allows us to extract portions of sequences of elements (such as 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 sequence.
The basic syntax for creating a slice in Python is as follows:
sequence[start:end:step]
start
: Index where the slice begins (inclusive)end
: Index where the slice ends (exclusive)step
: Size of the step or increment between elements of the slice (optional)
Special Considerations
Omitting Values
We can omit one, several, or even all parameters of the slice. Each omission will behave differently.
If we do not specify:
- The
start
, the slice will begin from the first element - The
end
, the slice will end at the last element - The
step
, the slice will use a step of1
first_except_three = numbers[3:] # Extracts elements from 3 to the end
first_three = numbers[:3] # Extracts the first three elements
See the examples below to fully understand the concepts 👇
Using Negative Indices
Python allows the use of negative indices to refer to elements relative to the end of the sequence (instead of from the beginning).
It seems a bit complicated and can be a little confusing at first. But basically,
last_three = numbers[-3:] # Extracts the last three elements
first_except_three = numbers[:-3] # Extracts elements, except the last three
On the other hand, if the step
is negative, the slice traverses the sequence in reverse order.
Again, see the examples below to fully understand the concepts 👇
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
Suppose 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, end, and step:
Without specifying start, end, 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 end
result = numbers[2:7]
print(result) # Result: [2, 3, 4, 5, 6]
Here, the slice starts at index 2 and ends at index 6 (exclusive), using the default step of 1
.
Specifying only end
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 (exclusive), 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 starts 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 list numbers
with a step of 2
, which means 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 starts at index 1, ends at index 8 (exclusive), and uses a step of 2
, taking every second element from the start to the specified end.
Negative Indices at the Start
When using negative indices at the start of the slice, elements are extracted 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 list numbers
.
Negative Indices at the End
It is 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 list numbers
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.
:::::::