Are all items in list the same?

Peter Otten __peter__ at web.de
Tue Jan 8 03:52:12 EST 2019


Tim Chase wrote:
> def all_equal(iterable):
> i = iter(iterable)
> first = next(i)
> return all(x == first for x in i)
>> It's undefined for an empty list (well, it throws a StopIteration
> but you can special-case that), but should hand the cases with
> 1 element and 2+ elements (both matching and where any item is not
> the same). It should work on an iterator as well but will consume the
> items in the process.
>> And I even like how nicely it reads :-)

If that's why you omitted the try...except use
first = next(i, None)


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /