Search code examples
pythonboolean-operationselementwise-operations

Logical AND operation across multiple lists


I have a dictionary that looks something like:

d= {'GAAP':[True,True],'L1':[True,False],'L2':[True,True]}

I would like to perform a logical AND operation across each of the values in the dictionary and return a LIST of True/False values. Something like:

for counter in range(0,2):
    print(d['GAAP'][counter] & d['L1'][counter] & d['L2'][counter])

My dictionary is fairly large so want to avoid manually typing each of the keys to perform the logical AND.


Solution

  • One way would be to use zip to get all corresponding elements and then to ask if they are all true:

    map(all, zip(*d.values()))
    

    Result it: [True, False]