There are also some other approaches to explore:
- there are
sets in Python (from documentation: "Basic uses include membership testing and eliminating duplicate entries."). Problem with solution below is that it will not short circuit (following code assumes that in case of empty matrix elements are considered equal as well, if its not the case comparison should be == 1):
In [1]: m = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
In [2]: len({el for row in m for el in row}) <= 1 # 0 when empty matrix, 1 when all equal
Out[2]: True
In [3]: n = [[1, 1, 1], [1, 1, 1], [1, 2, 1]]
In [4]: len({el for row in n for el in row}) <= 1
Out[4]: False
- Python has built-in itertools module and it's documentation contains
recipies where one of them is:
def all_equal(iterable):
"Returns True if all the elements are equal to each other"
g = groupby(iterable)
return next(g, True) and not next(g, False)