python reduce to check if all elements are equal
python, reduce
Solution
Try this instead, it works for lists of any size:
all(e == a[0] for e in a)
Notice that your proposed solution using `reduce` doesn't work for more than two items, as the accumulated value after the first comparison is `True`, and you'd be comparing `True` against each of the elements from that point on, and obviously that's not going to work.
Problem
Suppose `a = [[1,2,3],[1,2,3]]` `reduce(lambda x,y: x==y, a)` returns `True` But if `a = [[1,2,3],[1,2,3],[1,2,3]]` `reduce(lambda x,y: x==y, a)` returns `False` Why in the second case, the outcome is `False`? please help thanks