On Sun, May 1, 2011 at 2:28 PM, Greg Christian <glchrist...@comcast.net>wrote:
> Is there a way to write an if statement that will pick up duplicates > (two ‘1’s): > > L = ['1', '4', '1'] > if (L[0]) != (L[1]) != (L[2]): > print "THEY ARE NOT EQUAL" > else: > print "THEY ARE EQUAL" > > When I run this code, it prints “THEY ARE NOT EQUAL” when it *should*print > the else “THEY ARE EQUAL”. > > list L has two ‘1’s; therefore I am trying to get an if statement that will > recognize this. When using the != (not equal) operator, shouldn’t the if be > true when items in list are not the same? Any input would be appreciated. > > Thanks, > > Greg > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Learn about sets.: len(set(L)) == 1 is only true if they are all the same If you want to see if L[0] value is duplicated try if L[0] in set(L[1:]) if you want to see if there are any duplicates all all try len(L) != len(set(L)) -- Joel Goldstick
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor