[Tutor] all elements equal in tuple or list

Kent Johnson kent37 at tds.net
Fri Nov 19 03:28:33 CET 2004


The rest of the algorithm will fail if vals is None or an empty list or 
tuple. But an empty list, especially, should return True - all of the 
elements are the same. Both None and [] evaluate to False in a 
conditional so this skips the rest of the algoritm in those cases.

It will also return true if vals is the number 0 so maybe it is a bit 
over broad, but anyway that is the idea.

In general anything empty or 0 will evaluate to False in a conditional.

Kent

Dick Moores wrote:
> Kent, I'm having trouble getting my mind around this. Why do you use "if 
> vals"?
> 
> Thanks,
> 
> Dick
> 
> Kent Johnson wrote at 09:15 11/18/2004:
> 
>> def test(vals):
>>     if vals:
>>         i = iter(vals)
>>         first = i.next()
>>         for item in i:
>>             if first != item:
>>                 return False
>>     return True
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list