Steven Bethard <[EMAIL PROTECTED]> wrote:
> I have lists containing values that are all either True, False or None,
> e.g.:
>
> [True, None, None, False]
> [None, False, False, None ]
> [False, True, True, True ]
> etc.
>
> For a given list:
> * If all values are None, the function should return None.
> * If at least one value is True, the function should return True.
> * Otherwise, the function should return False.
>
> Right now, my code looks like:
>
> if True in lst:
> return True
> elif False in lst:
> return False
> else:
> return None
>
> This has a light code smell for me though -- can anyone see a simpler
> way of writing this?
What about...:
for val in lst:
if val is not None:
return val
return None
or the somewhat fancy/clever:
for val in (x for x in lst if x is not None):
return val
return None
Alex
--
http://mail.python.org/mailman/listinfo/python-list