tqdm show progress for a generator I know the length of

PythonGeneratorTqdm

Python Problem Overview


I'm looping over a large file that I know the length of, but am processing lazily since it's too large to fit in memory. I'd like to be able to use tqdm to keep track of my progress through the file, but since it can't get the total number of examples out of the generator I'm using, the only thing it shows is the estimated iterations/second. Is there any way to tell tqdm how many elements it's going to be looping over total so I can get some of the other statistics?

Python Solutions


Solution 1 - Python

You can pass the length to the argument total to make it work.

Example:

from tqdm import tqdm
    
length = 1000000
generator = (3 * n for n in range(length))  # just doing something random
for n in tqdm(generator, total=length):
    pass

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeorgeView Question on Stackoverflow
Solution 1 - PythonO. EdholmView Answer on Stackoverflow