Because i in lambda is probably not what you expect. To check this, change the code:
acts.append(lambda x: (i, i ** x))
Now print tells you the value of i :
(4, 16) (4, 16) (4, 16) (4, 16) (4, 16)
This means that lambda does not copy the value of i , but retains a reference to the variable, so all lambda see the same value. To fix this, copy i :
acts.append(lambda x, i=i: (i, i ** x))
Little i=i creates a local copy of i inside lambda .
[EDIT] Now why is this? In versions of Python prior to 2.1, local functions (i.e. functions defined inside other functions) could not see variables in the same scope.
def makeActions(): acts=[] for i in range(5): print len(acts) def f(x):
then you will get error i . lambda could see the covering area due to a somewhat weirder syntax.
This behavior has been fixed in one of the latest versions of Python (2.5, IIRC). Using these old versions of Python you will need to write:
def f(x, i=i):
Since the fix is (see PEP 3104 ), f() can see variables in the same area, so lambda no longer needed.