When I execute this code in python 2.6
reduce(lambda x,y: x+[y], [1,2,3],[])
I get [1, 2, 3] as expected.
But when I execute this one (I think it is equivalent to previous)
reduce(lambda x,y: x.append(y), [1,2,3],[])
I get an error message
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
AttributeError: 'NoneType' object has no attribute 'append'
Why these two lines of code do not give the same result?
x.append(y)is not equivalent tox+[y];appendmodifies a list in place and returns nothing, whilex+[y]is an expression that returns the result.