How to check all elements of a list are same or different
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. Thanks! Gaurav -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check all elements of a list are same or different
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! Thanks! Gaurav On Wed, Apr 15, 2009 at 5:49 PM, Chris Rebert wrote: > On Wed, Apr 15, 2009 at 2:36 PM, Gaurav Moghe 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 > -- Gaurav Moghe Graduate Student Evolutionary Genomics Lab Dept of Plant Biology Michigan State University USA -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check all elements of a list are same or different
Thanks! That works! On Wed, Apr 15, 2009 at 6:14 PM, Chris Rebert wrote: > > On Wed, Apr 15, 2009 at 5:49 PM, Chris Rebert wrote: > >> > >> On Wed, Apr 15, 2009 at 2:36 PM, Gaurav Moghe 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 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. > > Cheers, > Chris > -- > I have a blog: > http://blog.rebertia.com > -- http://mail.python.org/mailman/listinfo/python-list