2

Just curious to know if anyone knows of an inbuilt version of:

bool_list: List[bool] = # some non-empty list of booleans
result = all(bool_list) or not any(bool_list)

# What I'm looking for
result = allsame(bool_list)
Alexander Soare
  • 2,825
  • 3
  • 25
  • 53

1 Answers1

1

Not a built-in but does exactly what you are looking for in an efficient way:

from itertools recipes:

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)
Gábor Fekete
  • 1,343
  • 8
  • 16