MediumPython

itertools — Compose & Transform Iterators

PythonItertoolsFunctional

Implement solve(lists, n) that:
1. Chains all sub-lists into a single sequence with itertools.chain.from_iterable.
2. Returns the first n elements that are even, as a list.

Use itertools.islice to avoid materializing the full sequence.

Examples

  • solve([[1,2,3],[4,5,6],[7,8]], 3)[2, 4, 6]
  • solve([[1,3,5],[7,9]], 2)[] *(no evens)*
  • solve([[2,4,6,8]], 2)[2, 4]

Constraints

  • Do not use a plain for-loop to collect results — use itertools combinators.

Sample tests

Test #1Three sub-lists, first 3 evens
Input: [[[1,2,3],[4,5,6],[7,8]],3]
Output: [2,4,6]
Test #2No evens at all
Input: [[[1,3,5],[7,9]],2]
Output: []
Test #3Take only 2 from 4 evens
Input: [[[2,4,6,8]],2]
Output: [2,4]