Solution:
You can easily replace any element in a list of python by using a for-loop. You’ll check the particular element in the for-loop and replace it. Let’s see how to do it.
>>> x= [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
>>> for n, i in enumerate(x):
... if i == 5:
... x[n] = 10
...
>>> x
[1, 2, 3, 4, 10, 10, 4, 3, 2, 1]
If you want to replace more than one element in a list you can use a dictionary. Let’s check the program below:
x = [1, 2, 3, 4, 1, 5, 3, 2, 1]
dic = {1:5, 2:10, 3:'gavin'}
print([dic.get(n, n) for n in x])
> [5, 10, 'gavin', 4, 5, 5, 'gavin', 10, 5]
This may help you. Thanks