1
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]

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.