I would suggest using any and all . I would say that the advantage of this is readability rather than skill or effectiveness. For instance:
>>> vals0 = [0, 0, 0, 0, 0] >>> vals1 = [1, 1, 1, 1, 1] >>> vals2 = [0, 1, 0, 1, 0] >>> def category(vals): ... if all(vals): ... return 1 ... elif any(vals): ... return 2 ... else: ... return 0 ... >>> category(vals0) 0 >>> category(vals1) 1 >>> category(vals2) 2
This can be reduced a little if you want:
>>> def category(vals): ... return 1 if all(vals) else 2 if any(vals) else 0 ...
This works with everything that __nonzero__ (or __bool__ in Python 3) can interpret as having a true or false value.
source share