Here is a list in Python,
list = ["Red", "Blue", "Green", "White","Yellow"]
I want to replace "Blue" and "Green" with "gray" and "silver." How can I do this in Python?
python range of values
To change the specific range of values in a list, refer to the index range of that particular value.
list = ["Red", "Blue", "Green", "White","Yellow"]
list[1:3] = ["gray", "silver"]
print(list)
Suppose if you insert more items, your output will be like this,
list = ["Red", "Blue", "Green", "White","Yellow"]
list[1:3] = ["gray", "silver","silver","silver","silver","silver","silver"]
print(list)
OUTPUT: ['Red', 'gray', 'silver', 'silver', 'silver', 'silver', 'silver', 'silver', 'White', 'Yellow'
Suppose if you insert only one list element into the range of Values; your output will be like this
list = ["Red", "Blue", "Green"]
list[1:3] = ["gray"]
print(list)
O/P: ['Red', 'gray']