This code prints 2 2 2 instead of 1 2 3. I'd understand this result if k being referred to is shared between each list items, but I couldn't be certain about it.
flist = [(lambda x: k) for k in range(3)]
print(flist[0](0), flist[1](0), flist[2](0))
print(k)  # Fails as expected
- How does list comprehension work exactly? On what namespace does kexist?
- How should I change the code to get 1 2 3with about the same code complexities/length as list comprehension? I'm hardcoding list of flists, and I don't want my code to be excessively long.
 
     
    