PY-12

Keep Selected Values and Their Positions

  • Easy–Medium
  • Sequences and Selection
  • Python

Task

Write select_with_positions(values, keep_flags). The two input lists have the same length. Read them from left to right and keep a value exactly when the corresponding item in keep_flags is True.

Return a new list of (index, value) tuples. Each index is the original zero-based position in values; each value is the value from that position. Keep the original order. Do not change either input list.

Example

The position is retained even though the result contains only selected values. If no flag is True, return an empty list.

Your implementation

Edit solution.py and keep this function name and signature:

Return the required list; do not print it or ask for input. The input lists are valid for every call, so you do not need to define an error case for unequal lengths.