This code demonstrates the problem well:
ans = []
for x in range(1,10):
ans.append(lambda: x)
# -----------------------
# now see what happened:
ans[0]()
# prints 9, but should print 1
What is the best way to have x be captured immediately in the function, rather than updating with the variable x later?