Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

2 views
Skip to first unread message

Chris Rebert

unread,
Apr 15, 2009, 5:49:28 PM4/15/09
to Gaurav Moghe, pytho...@python.org
On Wed, Apr 15, 2009 at 2:36 PM, Gaurav Moghe <mogh...@msu.edu> wrote:
> Hi,
>
> I am an amateur python user I wanted to know how do I know whether all the
> contents of a list are all same or all different? Now, I could certainly
> write a loop with a counter. But is there a ready command for that? Checked
> a lot of docs and this mailing list, but didnt get anything worthwhile.
> Would be glad to know.

All same:

list_1 == list_2

All different:

all(x != y for x, y in zip(list_1, list_2))

Cheers,
Chris
--
I have a blog:
http://blog.rebertia.com

Chris Rebert

unread,
Apr 15, 2009, 6:14:11 PM4/15/09
to Gaurav Moghe, pytho...@python.org
> On Wed, Apr 15, 2009 at 5:49 PM, Chris Rebert <cl...@rebertia.com> wrote:
>>
>> On Wed, Apr 15, 2009 at 2:36 PM, Gaurav Moghe <mogh...@msu.edu> wrote:
>> > Hi,
>> >
>> > I am an amateur python user I wanted to know how do I know whether all
>> > the
>> > contents of a list are all same or all different? Now, I could certainly
>> > write a loop with a counter. But is there a ready command for that?
>> > Checked
>> > a lot of docs and this mailing list, but didnt get anything worthwhile.
>> > Would be glad to know.
>>
>> All same:
>>
>> list_1 == list_2
>>
>> All different:
>>
>> all(x != y for x, y in zip(list_1, list_2))
>>
On Wed, Apr 15, 2009 at 2:55 PM, Gaurav Moghe <mogh...@msu.edu> wrote:
> Hi Chris,
>
> Thanks for the reply. But I am interested in analysing the contents of just
> one list. For example,
>
> list1=[1,2,3,4,5,6]
> So, the logical statement would probably be:
> if list1==(contains all same values), print "Same" ---->False
> if list1==(contains all different values), print "Different" ---->True
>
> I wanted to know here whether there is a command/function that can do
> exactly this. I hope I am more clearer than my last try!

Ah, okay. Then you want:

def all_same(lst):
return len(set(lst)) == 1

def all_different(lst):
return len(set(lst)) == len(lst)

Note that these require all the elements of the list to be hashable.

John Posner

unread,
Apr 15, 2009, 6:56:45 PM4/15/09
to Chris Rebert, Gaurav Moghe, pytho...@python.org
Chris Rebert wrote:
> Ah, okay. Then you want:
>
> def all_same(lst):
> return len(set(lst)) == 1
>
> def all_different(lst):
> return len(set(lst)) == len(lst)
>
> Note that these require all the elements of the list to be hashable.
>
This solution relies on the object ID -- no hashability required:

# get list of object-IDs
ids = map(lambda x: id(x), mylist)
# ALL THE SAME? ... test whether "average ID" matches "first ID"
sum(ids)/len(ids) == ids[0]

Scott David Daniels

unread,
Apr 15, 2009, 7:15:13 PM4/15/09
to
John Posner wrote:
> ...This solution relies on the object ID -- no hashability required:

> # get list of object-IDs
> ids = map(lambda x: id(x), mylist)
> # ALL THE SAME? ... test whether "average ID" matches "first ID"
> sum(ids)/len(ids) == ids[0]

Assuming you really are going for "is" comparison, how about:
max(id(x) for x in mylist) == min(id(x) for x in mylist)

It has the advantage that if [id(x) for x in mylist] = [2N, 1N, 3N],
you get the answer you desire.

--Scott David Daniels
Scott....@Acm.Org

Rhodri James

unread,
Apr 15, 2009, 7:13:52 PM4/15/09
to pytho...@python.org
On Wed, 15 Apr 2009 23:56:45 +0100, John Posner <jjpo...@snet.net> wrote:

> Chris Rebert wrote:
>> Ah, okay. Then you want:
>>
>> def all_same(lst):
>> return len(set(lst)) == 1
>>
>> def all_different(lst):
>> return len(set(lst)) == len(lst)
>>
>> Note that these require all the elements of the list to be hashable.
>>

> This solution relies on the object ID -- no hashability required:
>
> # get list of object-IDs
> ids = map(lambda x: id(x), mylist)
> # ALL THE SAME? ... test whether "average ID" matches "first ID"
> sum(ids)/len(ids) == ids[0]

Oh John! After all the recent discussion of identity versus equality too.

>>> my_list = [ 1024 ]
>>> my_list.append(1024) # Defeat the interpreter's cunningness
>>> ids = map(lambda x: id(x), ml)
>>> ids
[9864656, 9864704]
>>> sum(ids)/len(ids) == ids[0]
False


--
Rhodri James *-* Wildebeeste Herder to the Masses

John Posner

unread,
Apr 15, 2009, 7:54:53 PM4/15/09
to Scott David Daniels, pytho...@python.org
Scott David Daniels wrote:
> Assuming you really are going for "is" comparison, how about:
> max(id(x) for x in mylist) == min(id(x) for x in mylist)
>
> It has the advantage that if [id(x) for x in mylist] = [2N, 1N, 3N],
> you get the answer you desire.
>
Oops -- got me! -John

John Machin

unread,
Apr 15, 2009, 8:33:33 PM4/15/09
to
On Apr 16, 8:14 am, Chris Rebert <c...@rebertia.com> wrote:
> > On Wed, Apr 15, 2009 at 5:49 PM, Chris Rebert <c...@rebertia.com> wrote:

>
> >> On Wed, Apr 15, 2009 at 2:36 PM, Gaurav Moghe <moghe...@msu.edu> wrote:

> > Thanks for the reply. But I am interested in analysing the contents of just
> > one list. For example,
>
> > list1=[1,2,3,4,5,6]
> > So, the logical statement would probably be:
> > if list1==(contains all same values), print "Same"    ---->False
> > if list1==(contains all different values), print "Different"  ---->True
>
> > I wanted to know here whether there is a command/function that can do
> > exactly this.  I hope I am more clearer than my last try!

@ OP: Not much ... what do you want to print if it contains *some*
different (but not all)?


> Ah, okay. Then you want:
>
> def all_same(lst):
>     return len(set(lst)) == 1
>
> def all_different(lst):
>     return len(set(lst)) == len(lst)

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

Paul Rubin

unread,
Apr 15, 2009, 8:48:00 PM4/15/09
to
John Posner <jjpo...@snet.net> writes:
> # get list of object-IDs
> ids = map(lambda x: id(x), mylist)
> # ALL THE SAME? ... test whether "average ID" matches "first ID"
> sum(ids)/len(ids) == ids[0]

I don't think you can rely on id's being the same if what
you want is that the values are the same:

>>> a = "foo" + "bar"
>>> b = "foobar"
>>> a==b
True
>>> id(a) == id(b)
False

I'd use:

from operator import eq
all_the_same = reduce(eq, mylist)

I don't see how to do all_different in less than quadratic time,
without using hashing or sorting:

all_different = all(sum(1 for y in mylist if x==y)==1 for x in mylist)

Miles

unread,
Apr 15, 2009, 9:29:11 PM4/15/09
to pytho...@python.org
On Wed, Apr 15, 2009 at 8:48 PM, Paul Rubin wrote:
> I'd use:
>
>   from operator import eq
>   all_the_same = reduce(eq, mylist)

That won't work for a sequence of more than two items, since after the
first reduction, the reduced value that you're comparing to is the
boolean result:

>>> reduce(eq, [0, 0, 0])
False
>>> reduce(eq, [0, 1, False])
True

I'd use:

# my preferred:
def all_same(iterable):
it = iter(iterable)
first = it.next()
return all(x == first for x in it)

# or, for the pathologically anti-generator-expression crowd:
from functools import partial
from operator import eq
from itertools import imap
def all_same(iterable):
it = iter(iterable)
return all(imap(partial(eq, it.next()), it))

-Miles

Piet van Oostrum

unread,
Apr 16, 2009, 5:25:10 AM4/16/09
to
>>>>> John Machin <sjma...@lexicon.net> (JM) wrote:

>JM> On Apr 16, 8:14 am, Chris Rebert <c...@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 <pi...@cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: pi...@vanoostrum.org

Arnaud Delobelle

unread,
Apr 16, 2009, 5:57:43 AM4/16/09
to
Piet van Oostrum <pi...@cs.uu.nl> writes:

>>>>>> John Machin <sjma...@lexicon.net> (JM) wrote:
>
>>JM> On Apr 16, 8:14 am, Chris Rebert <c...@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!

I didn't see the simple:

>>> def all_same(l):
... return all(l[i]==l[i+1] for i in range(len(l)-1))
...
>>> all_same([1,2,3])
False
>>> all_same([1])
True
>>> all_same([1,1,1])
True
>>> all_same([1,1,1,2])
False
>>> all_same([])
True
>>>

--
Arnaud

0 new messages