easy +10 pts

Boolean Mask Filter

Filter a list using a parallel boolean mask and return matching elements.

Write a function `filter_by_mask(arr, mask)` that takes two lists of the same length: `arr` containing numbers and `mask` containing booleans. The function should return a new list containing only the elements from `arr` where the corresponding boolean in `mask` is `True`, preserving the original order. Do not modify the input lists.

Constraints

- Both `arr` and `mask` are Python lists. - `len(arr) == len(mask)`. - `arr` may be empty. - `mask` elements are booleans. - Time complexity O(n), space O(n) for the result.

Example

```python
>>> arr = [10, 20, 30, 40]
>>> mask = [True, False, True, False]
>>> filter_by_mask(arr, mask)
[10, 30]

>>> arr = [1, 2, 3]
>>> mask = [False, False, False]
>>> filter_by_mask(arr, mask)
[]

>>> arr = [5, -1, 0]
>>> mask = [True, True, True]
>>> filter_by_mask(arr, mask)
[5, -1, 0]
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate over pairs using zip(arr, mask).
Use a list comprehension: [x for x, keep in zip(arr, mask) if keep].
If mask is True, keep the element; otherwise skip it.
For an empty arr, the result should be an empty list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.