How to show a progress bar in Python using tqdm

Home / python / How to show a progress bar in Python using tqdm
Published on September 9, 2026
Updated on September 24, 2026
4 min read

problem

Python doesn’t really support showing progress bars out of the box. But, we can use the tqdm library to show a pretty cool progress bar in the console. The progress bar can be formatted, e.g. to a specific color.

SOLUTION

The progress bar needs an Iterable collection to progress – iterate over. For our example, for simplicity we will create a list of numbers that the progress bar will iterate over and show the progress accordingly. 

Let’s create a list of 20 numbers using list comprehension.

progress_bar.py
				
					numbers = [x for x in range(20)]

				
			

To create a progress bar we simply call tqdm constructor and assign it to a local variable so we can control it after instantiation.

For example:

progress_bar.py
				
					bar = tqdm(numbers) #pass the iterable in the constructor 
				
			

Now, our progress bar is in the variable bar. We can iterate over this variable using a for loop and put a small delay on each iteration to avoid filling up the progress bar too quickly.

For the delay, we will use the sleep() function.

progress_bar.py
				
					
for n in bar:
   sleep(0.1) #small delay in seconds

				
			
running complete code
progress_bar.py
				
					
from tqdm import tqdm
from time import sleep

numbers = [x for x in range(20)]
bar = tqdm(numbers)

for n in bar: #loop to fill up the progress bar
    sleep(0.1)

				
			
output

Loading…

tqdm in python example

After loading – done

tqdm default format example

In this screenshot we can see the default format of the progress bar. It shows a percentage, then the bars, then 20/20 (20 items in numbers list), followed by some other data in square brackets.

Let’s customize it a bit.

Change the color

To change the color to blue, we do the following:

progress_bar.py
				
					bar.colour="blue"

				
			
running complete code
progress_bar.py
				
					
from tqdm import tqdm
from time import sleep

numbers = [x for x in range(20)]
bar = tqdm(numbers)
bar.colour = "blue" #sets to Blue

for n in bar:
    sleep(0.1)

				
			
output

Now, the progress bar is blue. 

tqdm example of colour blue

If we put a color that is not in that list, e.g. purple:

progress_bar.py
				
					
from tqdm import tqdm
from time import sleep

numbers = [x for x in range(20)]
bar = tqdm(numbers)
bar.colour="PURPLE" # UNSUPPORTED colour

for n in bar:
    sleep(0.1)

				
			
output - error

The progress bar will be empty and it will show the following error:

Adding a custom colour

To specify a custom colour, we can use a HEX code as follows:

progress_bar.py
				
					
from tqdm import tqdm
from time import sleep

numbers = [x for x in range(20)]
bar = tqdm(numbers)
bar.colour="#D4F3CE" # HEX code of custom colour


for n in bar:
    sleep(0.1)

				
			
output
tqdm custom color with hex code
For fun - changing colours of progress bars
tqdm progress bar changing gradient color example
tqdm progress bar changing colours example
progress_bar.py
				
					from tqdm import tqdm
from time import sleep

colors = ["#00ff00", "#D4F3CE", "BLACK", "RED", "GREEN", "YELLOW", "BLUE", "MAGENTA", "CYAN", "WHITE"]
numbers = [x for x in range(50)]
bar = tqdm(numbers)
i = 0

for char in bar:
    sleep(0.1)
    colour = colors[i % len(colors)] # pick next color repeadetly (first -> last -> repeat)
    bar.colour = colour
    i += 1

				
			

conclusion

In this post we saw how to use the tqdm package and show a progress bar in the console of our Python program.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies (Ads)

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings. We use Google Ads on this site.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.