In Python, list slicing means taking some part from the original list.
Slicing is also called Range of Indexes.
To slice the list, we provide the start item index position, end item index position, and step count separated by a colon.
list[ 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 list with items between the start and end indexes.
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[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 list 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 list.
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[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 list will cover all items after the start item index from the original list (["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_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[: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 list will cover all items before the end item index from the original list (["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 list with items between the start and end indexes.
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[-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 list 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 list, so it covers all items from that specified index position forward.
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[-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 list will cover all items after the start item index from the original list (['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_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[:-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 list will cover all items before the end item index from the original list (['Red', 'Blue', 'Green', 'Orange', 'White']
).
While slicing, you can also specify both positive and negative items positions.
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[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 list.
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[-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 list.
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_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[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 list (means items in reverse format).
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[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_list = ["Red", "Blue", "Green", "Orange"]
print(colors_list[::-1])
['Orange', 'Green', 'Blue', 'Red']
In the output, you can see that slicing with the negative step reverses all items.
You can change specific items from the list using the slicing, define a new list with items and assign that new list to the original list with the index number.
colors_list = ["Red", "Blue", "Green", "Orange", "White"]
new_colors_list = ["Purple", "Grey"]
colors_list[2:4] = new_colors_list
print(colors_list)
['Red', 'Blue', 'Purple', 'Grey', 'White']
In the above example, we change the color "Green"
and "Orange"
to "Purple"
and "Grey."
. Here we declare a new list (new_colors_list
) with the colors "Purple"
and "Grey"
and assign that list to our original color list (colors_list
) using index positions 2
to 4
.
If you insert more items than specific replace items, then after the new items insert into the specified item positions and remaining items move accordingly.
colors_list = ["Red", "Blue", "Green"]
new_colors_list = ["Purple", "Grey"]
colors_list[1:2] = new_colors_list
print(colors_list)
['Red', 'Purple', 'Grey', 'Green']
In the above example, we change the color "Blue"
to "Purple"
, but the color "Grey"
is new to the list on item position 2, and because of this reason "Green"
color shifted from item position 2
to 3
.
As we previously learned, performing a slicing then includes the start position item and excludes the end position item, so the color "Grey"
does not replace the color "Green"
.
If you insert fewer items than specific replace items, then after the new items insert into the specified item positions and remaining items move accordingly.
colors_list = ["Red", "Blue", "Green"]
new_colors_list = ["Purple"]
colors_list[1:3] = new_colors_list
print(colors_list)
['Red', 'Purple']
The color "Blue"
changes to "Purple"
, but the color "Green"
delete from the list at item position 2
.
A reason to delete the color "Green"
from the list is we set the index range from 1
to 3
, and the index position of the "Green"
color item is 2
, so it removed from the list.
You can delete list items from the middle of the list by assigning an empty list to the original list with start and end item index positions.
colors_list = ["Red", "Blue", "Green"]
new_colors_list = []
colors_list[1:3] = new_colors_list
print(colors_list)
['Red']
In the above example, we set the start index position 1
, and the end index position 3
in the slice and assign an empty list to that slice, so the result colors "Blue"
(1
) and "Green"
(2
) delete from the original list.
The "Red"
color remains in the list because the index position of the "Red"
color is 0
.
You can also delete items slice from the list using the del
keyword.
colors_list = ["Red", "Blue", "Green"]
del colors_list[1:3]
print(colors_list)
['Red']
Here we assigned a list slice with the start index position 1
, and the end index position 3
to the del
keyword, so the result deletes the "Blue"
and "Green"
colors from the list.