A programmer needs to check if at least one element in a list of booleans flags is True. Which expression correctly does this?
Trap 1: True in flags
This checks membership using the `in` operator. For a list of booleans, it works, but it only looks for the exact boolean value `True`, not for general truthy values. It is not the recommended way to check if any element is true.
Trap 2: all(flags)
`all(flags)` returns `True` only if every element is truthy, not if at least one is true.
Trap 3: flags.any()
`flags.any()` is not a valid Python method for lists; it exists in libraries like NumPy but not in standard Python.
- A
True in flags
Why wrong: This checks membership using the `in` operator. For a list of booleans, it works, but it only looks for the exact boolean value `True`, not for general truthy values. It is not the recommended way to check if any element is true.
- B
all(flags)
Why wrong: `all(flags)` returns `True` only if every element is truthy, not if at least one is true.
- C
flags.any()
Why wrong: `flags.any()` is not a valid Python method for lists; it exists in libraries like NumPy but not in standard Python.
- D
any(flags)
`any(flags)` is the correct built-in function that returns `True` if any element in the iterable is truthy. For a list of booleans, this is the proper way to check if at least one is `True`.