Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does tqdm's total parameter do?

Tags:

python

tqdm

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)
like image 931
Mint.K Avatar asked May 20 '18 02:05

Mint.K


People also ask

What is the function of tqdm?

tqdm is a library in Python which is used for creating Progress Meters or Progress Bars. tqdm got its name from the Arabic name taqaddum which means 'progress'.

What is tqdm progress bar?

In Arabic, tqdm (taqadum) means progress, and it is used to create a smart progress bar for the loops. You just need to wrap tqdm on any iterable - tqdm(iterable).

What is Trange in tqdm?

trange(i) is a special optimised instance of tqdm(range(i)): from tqdm import trange for i in trange(100): sleep(0.01)

How fast is tqdm?

tqdm is the default iterator. It takes an iterator object as argument and displays a progress bar as it iterates over it. You can see the nice output with 9.90it/s meaning an average speed of 9.90 iterations per second.


1 Answers

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

like image 81
user3483203 Avatar answered Oct 12 '22 17:10

user3483203