def foo():
    lst = []
    for i in range(4):
        lst.append(lambda: i)
        print(lst[i]())
    print([f() for f in lst])
foo()
this is the output for above code
0
1
2
3
[3, 3, 3, 3]
why the lst inside the for loop is printing [0,4) and lst outside the for loop is printing [3,3,3,3]
 
    