Search code examples
pythontqdm

What does tqdm's total parameter do?


What's the difference between the two? tqdm wraps around any iterable. But I am not sure how tqdm functions when it's given two arguments.

# train_ids = list
elements = ('a', 'b', 'c')
for count, ele in tqdm(enumerate(elements)):
    print(count, i)
# two arguments
for count, ele in tqdm(enumerate(elements), total=len(train_ids)):
    print(count, i)

Solution

  • 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