Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why python reduce() behaves differently with the `None` element?

I don't understand the answers for a similar question.

It is clear that this should return True

l = [1,1,1]
reduce(lambda x,y: x== y, l)

However, how do you explain this retuns False (when None==None is True)

l = [None,None,None]
reduce(lambda x,y: x== y, l)
like image 733
rds Avatar asked Dec 13 '25 01:12

rds


1 Answers

Because

1 == True # 1 == (1 == 1)

is True, but

None == True # None == (None == None)

is False (and None == False is False as well, so once you got False, it stays False).

That's how reduce works: It passes each element and the result of the previous evaluation to the callback. And by that it reduces a sequence of values to one value.

like image 146
Felix Kling Avatar answered Dec 14 '25 22:12

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!