Straight from the documentation:
If the optional variable total (or an iterable with len()) is provided, predictive stats are displayed.
Also from the documentation:
total
: int
, optional .
The number of expected iterations. If (default: None), len(iterable)
is used if possible. As a last resort, only basic progress statistics
are displayed (no ETA, no progressbar). If gui is True and this
parameter needs subsequent updating, specify an initial arbitrary
large positive integer, e.g. int(9e9).
When you provide total
as a parameter to tqdm
, you are giving it an estimate for how many iterations the code should take to run, so it will provide you with predictive information (even if the iterable you have provided does not have a length).
Example
If we provide a generator (something without a __len__
) to tqdm
without a total
argument, we don't get a progress bar, we just get elapsed time:
no_len = (i for i in range(50))
for i in tqdm(no_len):
time.sleep(0.1)
# Result
19it [00:01, 9.68it/s]
However, if we use the total
parameter to give expected iterations, tqdm
will now estimate progress:
for i in tqdm(no_len, total=49):
time.sleep(0.1)
# Result
94%|████████████████████████████████████████▎ | 46/49 [00:04<00:00, 9.72it/s
In addition to the total
parameter, tqdm
has a whole set of additional parameters that you can find here