# option 1
for i, item in enumerate(lst):
if item == 2:
lst[i] = 7
# option 2
counter = 0
for i in lst:
if i == 2:
lst[counter] = 7
counter += 1
# option 3
for i in range(len(lst)):
if lst[i] == 2:
lst[i] = 7
lst = [1, 2, 3, 4]
def replace(x,y=2,z=7):
"""Replace value if condition holds.
Keyword arguments:
x -- value to check for replacement
y -- x will be replaced if it has the value of y
z -- x will be replaced by z if x is equal to y
"""
if(x==y):
x=z
return x
lst = [replace(x,2,7) for x in lst]