Home > database >  List comprehension with boolean (Python)
List comprehension with boolean (Python)

Time:12-16

How to present this code as a list comprehension:

def all_targets_hit(attempts: list) -> bool:

    for ls in range(len(attempts)):
        if not any(attempts[ls]):
            return False
    return True

attempts =([
            [True, False, False],
            [False, False, True],
            [False, False, False, False],
        ]) 

#Expected: False

CodePudding user response:

You could combine all with any:

def all_targets_hit(attempts: list) -> bool:
    return all([any(sublist) for sublist in attempts])

attempts =([
            [True, False, False],
            [False, False, True],
            [False, False, False, False],
        ]) 

all_targets_hit(attempts)
  • Related