In Python, tuple slicing means taking some part from the original tuple.
Slicing is also called Range of Indexes.
To slice the tuple, we provide the start item index position, end item index position, and step count separated by a colon.
tuple[ start : end : step ]
In the slicing, you can specify where to start and where to end, and when you do that, it returns a new tuple with items between the start and end indexes.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[1:5])
('Blue', 'Green', 'Orange', 'White')
In the above example, we set the start index range from 1
, and the end index is 5
, resulting in a new tuple with the following items (1) "Blue", (2) "Green", (3) "Orange", (4) "White"
.
The output includes the index position 1
("Blue"
) item but excludes the index position 5
("Black"
) item.
Whenever you perform a slicing, it includes the start position item and excludes the end position item. Remember that the index position of the first item is 0
.
In the slicing, when you specify where to start and ignore the end item position, automatically it goes to the end of the tuple.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[3:])
('Orange', 'White', 'Black', 'Yellow')
In the above example, we specify the start item position (3
) and ignore the end item position so that the new tuple will cover all items after the start item index from the original tuple (("Orange", "White", "Black", "Yellow")
).
In the slicing, when you ignore where to start and specify only the end item position, the indexes will begin from the first item and goes up to the specified index position.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[:4])
('Red', 'Blue', 'Green', 'Orange')
In the above example, we ignore the start item position and specify the end item position (4
) so that the new tuple will cover all items before the end item index from the original tuple (("Red", "Blue", "Green", "Orange")
).
In the slicing with negative indexes, you can specify where to start and where to end (negative indexing starts picking items from the end), and when you do that, it returns a new tuple with items between the start and end indexes.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[-5:-2])
('Green', 'Orange', 'White')
In the above example, the negative index position for the start item is -5
, and the end index position is -2
, resulting in a new tuple with the following items (-5) "Green", (-4) "Orange", (-3) "White"
. The output includes the index position -5
("Green"
) item but excludes the index position -2
("Black"
) item.
In the slicing with a negative index, when you specify where to start (with a minus sign with an integer value) and ignore the end item position, the slice goes to the end of the tuple, so it covers all items from that specified index position forward.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[-5:])
('Green', 'Orange', 'White', 'Black', 'Yellow')
In the above example, we specify the start item position (-5
) and ignore the end item position so that the new tuple will cover all items after the start item index from the original tuple (('Green', 'Orange', 'White', 'Black', 'Yellow')
).
In the slicing with a negative index, when you ignore where to start and specify only the end item position (with a minus sign with an integer value), the indexes will begin from the first item and goes up to the specified index position.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[:-2])
('Red', 'Blue', 'Green', 'Orange', 'White')
In the above example, we ignore the start item position and specify the end item position (-2
) so that the new tuple will cover all items before the end item index from the original tuple (('Red', 'Blue', 'Green', 'Orange', 'White')
).
While slicing, you can also specify both positive and negative items positions.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[1:-3])
('Blue', 'Green', 'Orange')
In the above example, while slicing, we set the start index position 1
, and the end index position -3
, and as a result, we get the colors "Blue" (1)
, "Green" (2)
, and "Orange" (3)
from the original tuple.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[-3:6])
('White', 'Black')
In the above example, while slicing, we set the start index position -3
, and the end index position 6
, and as a result, we get the colors "White" (4)
, and "Black" (5)
from the original tuple.
Additionally, you can specify the step while slicing, which takes items at specific intervals from the specified start and ends index positions.
It is optional to set the step parameter. By default, the step is 1.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[2:7:2])
('Green', 'White', 'Yellow')
In the above example, we set the start index position 2
, and the end index position 7
with step 2
in the slice. In the output, we get items at the interval of 2.
With a negative step, we get items from the end to the start of the original tuple (means items in reverse format).
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[6:2:-2])
('Yellow', 'White')
In the above example, we set the start index position 6
, and the end index position 2
with step -2
in the slice in output get items at the interval of 2 but in reverse format.
Slicing with the negative step, we get items in reverse format.
colors_tuple = ("Red", "Blue", "Green", "Orange")
print(colors_tuple[::-1])
('Orange', 'Green', 'Blue', 'Red')
In the output, you can see that slicing with the negative step reverses all items.
You can not change specific items from the tuple using the slicing, if you try, you'll get a TypeError
.
colors_tuple = ("Red", "Blue", "Green", "Orange", "White")
new_colors_tuple = ("Purple", "Grey")
colors_tuple[2:4] = new_colors_tuple
print(colors_tuple)
TypeError: 'tuple' object does not support item assignment
In the above example, a type error (TypeError: 'tuple' object does not support item assignment
) occurs when we attempt to change a tuple.
There is no way to delete a slice from a tuple, if you try, you'll get a TypeError
.
colors_tuple = ("Red", "Blue", "Green")
del colors_tuple[1:3]
print(colors_tuple)
TypeError: 'tuple' object does not support item deletion
In the above example, a type error (TypeError: 'tuple' object does not support item deletion
) occurs when we attempt to delete a slice from a tuple.