Add progress bar to your for loops in Python

Devashish Patil
CodeByte
Published in
2 min readFeb 5, 2021
Photo by Markus Spiske on Unsplash

Suppose you are working with a significantly long runtime and contains multiple for loops. I am sure you must have added all the necessary logging to know the status of your running code.

While this is an absolutely good way to work on this type of use-cases, you can add a little beauty to your logs and give a treat to your eyes.

Here’s how you can add a progress bar to for loops in python.

First, you need to install a library called tqdm.

pip install tqdm

Add this to your iterators like this:

from tqdm import tqdmfor i in tqdm(range(100)):
# Write your code here
# .
# You can add this to iterators like lists as welllst = [1,2,3,4,5,6,7,8]for i in tqdm(lst):
#write your code here

If you just want to visualize it and see how it looks, you can add sleep of 1 second to each iteration like this.

I hope this helped and made your coding experience even better.

--

--