def replace(l, X, Y):
    for i, v in enumerate(l):
        if v == X:
            l.pop(i)
l.insert(i, Y)
l = [3, 1, 4, 1, 5, 9]
replace(l, 1, 7)
Python - Replacing element in list without list comprehension, slicing or using [ ]s