[Tutor] Compound if statement question.

Joel Goldstick joel.goldstick at gmail.com
Sun May 1 21:16:54 CEST 2011


On Sun, May 1, 2011 at 2:28 PM, Greg Christian <glchristian at 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 at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110501/591a462a/attachment.html>


More information about the Tutor mailing list