CodeByte
Published in

CodeByte

Add progress bar to your for loops in Python

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Devashish Patil

Devashish Patil

Cloud Engineer at Google | IITR Alumnus