In Python, you can change the value of an item in a list by using its index number.
colors_list = ["Red", "Blue", "Green"]
colors_list[1] = "Purple"
print(colors_list)
['Red', 'Purple', 'Green']
In the above example, we changed the value from "Blue"
to "Purple"
at the index position "1"
.
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"
.
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.
To add a new list at the beginning of the original list, set the end item index to 0
(the list index count starts at 0
).
colors_list = ["Red", "Blue", "Green"]
new_colors_list = ["Purple", "Grey"]
colors_list[:0] = new_colors_list
print(colors_list)
['Purple', 'Grey', 'Red', 'Blue', 'Green']
The above example sets the end item index position to 0
when adding a new list to the original list, resulting in the new list items appearing first in the original list.
Set the start index count as the length of the original list when adding a new list at the end.
colors_list = ["Red", "Blue", "Green"]
new_colors_list = ["Purple", "Grey"]
colors_list[len(colors_list):] = new_colors_list
print(colors_list)
['Red', 'Blue', 'Green', 'Purple', 'Grey']
The above example sets the start item index position to the length (len(colors_list)
) of the original list when adding a new list to the original list, resulting in the new list items appearing at the end of the original list.
To add a new list at the center of the original list, set the start and end item positions.
colors_list = ["Red", "Blue", "Green"]
new_colors_list = ["Purple", "Grey"]
colors_list[1:1] = new_colors_list
print(colors_list)
['Red', 'Purple', 'Grey', 'Blue', 'Green']
In the above example, we set the start and end index item positions as 1
, resulting in the new list items appearing in the middle of the original list.
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
.