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

2009-04-15 Thread John Posner
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 -- http://mail.python

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

2009-04-15 Thread Rhodri James
On Wed, 15 Apr 2009 23:56:45 +0100, John Posner 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 solut

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

2009-04-15 Thread John Posner
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: