[Tutor] all elements equal in tuple or list
Jacob S.
keridee at jayco.net
Fri Nov 19 03:24:32 CET 2004
My name's not Kent, but...
>def test(vals):
> if vals:
> i = iter(vals)
> first = i.next()
> for item in i:
> if first != item:
> return False
> return True
The if vals: is an interesting idea... If no comparison operators are
entered after a variable, the interpreter automatically checks to see if the
variable returns true. So if vals returns true, then the code block beneath
it is executed. Otherwise, the function returns True.
All that does is check to see that vals is not an empty list. (Therefore
returning False)
Empty tuples,lists,dictionaries,strings,etc. return False ex. (),[],{},""
If they have anything in them, they return True ex.
(''),["hello"],{"Jacob",3},"A"
Nonzero integers,floats,complex numbers,Long integers, etc. return True
All others return True
More information about the Tutor
mailing list