Basics of TQDM for Progress Bars

Learn how to get started with TQDM to create interactive loops in Python

Vardan Agarwal
DataSeries
2 min readMar 23, 2020

--

Photo by Volodymyr Hryshchenko on Unsplash

TQDM is a python library through which progress bars can be added to for loops to show their status. Whenever there was a time taking loop with lots of iterations we use a variable, increment it in each iteration and print its status to know the status of the loop. This is where tqdm comes in.

tqdm means “progress” in Arabic (taqadum, تقدّم)

Using tqdm we can create progress bars to show the number of iterations passed, the time taken till now and the time it should take to complete all the iterations.

Installation

It can be installed using pip:

pip install tqdm

If you are using anaconda the using conda:

conda install -c conda-forge tqdm

Usage

For Jupyter notebooks use tqdm.notebood otherwise just tqdm. I am using a Jupyter notebook so I will show using tqdm.notebook. To create a loop that goes until certain range use trange. For nested loops, we can assign them names for clarity using desc as a parameter.

from tqdm.notebook import trange
for i in trange(2, desc='1st loop'):
for j in trange(10000000, desc='2nd loop'):
pass

Iterating over lists and arrays is even easier using tqdm. All we need to do is to wrap them using tqdm().

from tqdm.notebook import tqdm
import numpy as np
from time import sleep
arr = np.random.randint(1,101,1000)
for i in tqdm(arr):
sleep(0.1)

For more on this awesome library refer to its documentation.

P.S. If the loop fails to execute due to an error or is interrupted in between the progress bar turns red.

--

--