tqdm progressbar and zip built-in do not work together

Russell Burdt picture Russell Burdt · Dec 15, 2016 · Viewed 9.3k times · Source

tqdm is a Python module to easily print in the console a dynamically updating progressbar. For example

from tqdm import tqdm
from time import sleep
for _ in tqdm(range(10)): 
    sleep(0.1) 

prints a dynamic progressbar in the console for 1sec as the iteration executes: enter image description here

I have not figured out how to use tqdm with the builtin zip object.
The use case of this would be to iterate over two corresponding lists with a console progressbar.
For example, I would expect this to work:

for _, _ in tqdm(zip(range(10), range(10))):
    sleep(0.1)

but the progressbar printed to the console in this case is not correct: enter image description here

A workaround is to use tqdm with enumerate, however then an iterator index must be defined and managed.

Answer

Russell Burdt picture Russell Burdt · Dec 16, 2016

tqdm can be used with zip if a 'total' keyword argument is provided in the tqdm call.

The following example demonstrates iteration over corresponding elements in two lists with a working tqdm progressbar for the case where a 'total' keyword argument is used. enter image description here

The issue is that tqdm needs to know the length of the iterable ahead of time. Because zip is meant to handle iterables with different lengths, it does not have as an attribute a single length of its arguments.

So, tqdm still works nicely with zip, you just need to provide a little manual control with the 'total' keyword argument.