Search code examples
pythonlistnestedbooleanlist-comprehension

List comprehension with boolean (Python)


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


Solution

  • 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)