How to apply a logical operator to all elements in a python list

后端 未结 6 1734
日久生厌
日久生厌 2021-01-29 22:25

I have a list of booleans in python. I want to AND (or OR or NOT) them and get the result. The following code works but is not very pythonic.

def apply_and(alist         


        
6条回答
  •  半阙折子戏
    2021-01-29 22:51

    Reduce can do this:

    reduce(lambda a,b: a and b, alist, True)
    

    As fortran mentioned, all is the most succinct way to do it. But reduce answers the more general question "How to apply a logical operator to all elements in a python list?"

提交回复
热议问题