def replace(l, X, Y):
    for v in l:
        i = l.index(v)
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