Russell Burdt
Russell Burdt

Reputation: 2673

access member of a for loop in tqdm desc keyword argument

Is it possible to access the current item in a for loop in the prefix of a progressbar (the desc keyword argument). That is, to get something like this to work:

from tqdm import tqdm
for x in tqdm(['a' ,'b', 'c'], desc='item {}'.format(x)):
    pass

That code produces a NameError: name 'x' is not defined at the line of the for loop. Could it be made instead to create a progressbar where the prefix updates with x in the for loop.

item a: 100%|███████████████████████████████████████| 1/3 [00:00<?, ?it/s]
item b: 100%|███████████████████████████████████████| 2/3 [00:00<?, ?it/s]
item c: 100%|███████████████████████████████████████| 3/3 [00:00<?, ?it/s]

Upvotes: 2

Views: 330

Answers (1)

natalie
natalie

Reputation: 204

Would something like this work?

pbar = tqdm(["a", "b", "c"])
for x in pbar:
    pbar.set_description('item {}'.format(x))

Upvotes: 3

Related Questions