← All Exercises

Python Itertools Chains

intermediatepython~20min

Background

Itertools provides memory-efficient tools for working with iterators, essential for large or infinite data streams.

Problem

Write a function that takes multiple lists, chains them into a single iterable, and returns the first N unique elements.

Input

Multiple lists of elements and a count N.

Output

A list of the first N unique elements from the chained iterables.

Sample tests

Sample 1

Input: [1,2], [2,3], [3,4], N=3
Expected: [1, 2, 3]

Your solution

Learning objective

Learn itertools.chain for lazy concatenation and efficient unique element extraction.