I like the function by EnricoGiampieri (above) but there is a simpler version using all_equal from the "Itertools recipes" section of the itertools docs:
from itertools import groupby
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)
All of these recipes are packaged in more_itertools:
Substantially all of these recipes and many, many others can be installed from the more-itertools project found on the Python Package Index:
pip install more-itertools
The extended tools offer the same high performance as the underlying toolset. The superior memory performance is kept by processing elements one at a time rather than bringing the whole iterable into memory all at once. Code volume is kept small by linking the tools together in a functional style which helps eliminate temporary variables. High speed is retained by preferring “vectorized” building blocks over the use of for-loops and generators which incur interpreter overhead.
from more_itertools import all_equal
all_equal(map(type, iterable))
or with isinstance and a known type int (as per the original question)
all_equal(map(lambda x: isinstance(x, int), iterable))
These two ways are more concise than Enrico's suggestion and handle 'void iterators' (e.g. range(0)) just as Enrico's function does.
all_equal(map(type, range(0))) # True
all_equal(map(type, range(1))) # True
all_equal(map(type, range(2))) # True
all_equal(map(lambda x: isinstance(x, int), range(0))) # True
all_equal(map(lambda x: isinstance(x, int), range(1))) # True
all_equal(map(lambda x: isinstance(x, int), range(2))) # True
allis the way... - linello