Steven Bethard 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?

STeVe

That code looks like a pretty solid implementation of the spec to me. There isn't a strict need for the last else, of course, which may be the smell you detect.


If you wanted to get clever you could write something like

for i in True, False:
  if i in lst:
    return i
return False

but frankly I think that's more obscure, and saves you pretty much nothing.

regards
 Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005                      http://www.pycon.org/
Steve Holden                           http://www.holdenweb.com/
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to