Unique id generator

EasyIterator & Generator Programs · 14 of 20

Write a generator that produces ids like TC-001, TC-002, …

Advertisement
Output
TC-001 TC-002 TC-003

Approach

  1. itertools.count gives an endless counter.

Solution

Python
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.

More iterator & generator programs

← All 20 Iterator & Generator Programs

Leave a Comment