I'm looking to create and store a specific function for each key of a dictionary. However, I've noticed that whenever I try to iterate through my set of keys (By creating an iterable then manually iterating, using a comprehension, and by the "for" loop), all of the functions turn out the same:
>>> numbers = list(range(10))
>>> functions = {}
>>> for number in numbers:
functions[number] = lambda: number
# I'd expected the call functions[2]() should return 2
>>> two_function = functions[2]
>>> two()
9
Could someone explain why this happens and how to fix this?