Python Practice › Iterator & Generator Programs › Program 14
Unique id generator
Write a generator that produces ids like TC-001, TC-002, …
Output
TC-001 TC-002 TC-003
Approach
itertools.countgives an endless counter.
Solution
from itertools import count def test_ids(prefix="TC"): for n in count(1): yield f"{prefix}-{n:03}" ids = test_ids()print(next(ids), next(ids), next(ids))Output
TC-001 TC-002 TC-003
Learn the concept
New to this topic? Read the lesson, then come back.