# Pythonic ways of checking if all
# items in a list are equal:
>>> lst = ['a', 'a', 'a']
>>> len(set(lst)) == 1
True
>>> all(x == lst[0] for x in lst)
True
>>> lst.count(lst[0]) == len(lst)
True
# I ordered those from "most Pythonic" to "least Pythonic"
# and "least efficient" to "most efficient".
# The len(set()) solution is idiomatic, but constructing
# a set is less efficient memory and speed-wise.
# Pythonic ways of checking if all
# items in a list are equal:
>>> lst = ['a', 'a', 'a']
>>> len(set(lst)) == 1
True
>>> all(x == lst[0] for x in lst)
True
>>> lst.count(lst[0]) == len(lst)
True
# I ordered those from "most Pythonic" to "least Pythonic"
# and "least efficient" to "most efficient".
# The len(set()) solution is idiomatic, but constructing
# a set is less efficient memory and speed-wise.
$ python3
Python 3.9.1 (default, Dec 24 2020, 16:53:18)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> help(all)
Help on built-in function all in module builtins:
all(iterable, /)
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
>>>