How to check all elements of a list are same or different

Piet van Oostrum piet at cs.uu.nl
Thu Apr 16 05:25:10 EDT 2009


>>>>> John Machin <sjmachin at lexicon.net> (JM) wrote:

>JM> On Apr 16, 8:14 am, Chris Rebert <c... at rebertia.com> wrote:
>>> 
>>> def all_same(lst):
>>>     return len(set(lst)) == 1
>>> 
>>> def all_different(lst):
>>>     return len(set(lst)) == len(lst)

>JM> @ OP: These are very reasonable interpretations of "all same" and "all
>JM> different" but of course can both return False for the same input.

They can even both return True for the same input!
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list