This article is part of in the series
Published: Friday 25th April 2025

tdqm python

In the world of Python programming, few packages combine simplicity and functionality as elegantly as TQDM. This powerful library has revolutionized how developers track progress in their code, making it an essential tool for data scientists, machine learning engineers, and Python developers alike. This article dives deep into TQDM, exploring its features, implementation, and best practices.

What is TQDM?

TQDM (pronounced "taqaddum") derives from the Arabic word تقدّم meaning "progress." This aptly named library creates smart progress bars for loops and iterative processes in Python. Instead of staring at a seemingly frozen terminal during long-running operations, TQDM provides visual feedback that shows exactly how much of a task has been completed and estimates the remaining time.

Why Use TQDM?

  • Minimal Code Changes: Add progress bars with just one line of code
  • Performance Optimized: Minimal overhead to your existing code
  • Rich Information: Shows progress percentage, elapsed time, and estimated time remaining
  • Customizable: Easy to adjust colors, formats, and displayed information
  • Cross-Platform: Works across different operating systems and environments
  • Multiple Interfaces: Supports command-line, Jupyter notebooks, and GUI applications

Installation

Getting started with TQDM is straightforward:

pip install tqdm

For Jupyter Notebook support:

pip install tqdm ipywidgets

Basic Usage

The simplest way to use TQDM is to wrap any iterable with the tqdm() function:

from tqdm import tqdm
import time

# Add a progress bar to a simple loop
for i in tqdm(range(100)):
    time.sleep(0.01)  # Simulate work

This single line addition transforms a standard loop into one with a progress bar that shows completion percentage, iteration speed, and estimated time remaining.

Advanced Features

Manual Control

For more complex scenarios, you can manually control the progress bar:

from tqdm import tqdm
import time

# Create a progress bar with total steps
progress_bar = tqdm(total=100)

# Update it manually
for i in range(100):
    time.sleep(0.01)  # Simulate work
    progress_bar.update(1)  # Increment by 1

progress_bar.close()  # Close the bar when done

Custom Descriptions

Add contextual information to your progress bars:

from tqdm import tqdm
import time

for i in tqdm(range(3), desc="Processing files"):
    for j in tqdm(range(100), desc=f"File {i+1}", leave=False):
        time.sleep(0.01)  # Simulate work

Progress Bars for Pandas Operations

TQDM integrates seamlessly with Pandas:

import pandas as pd
from tqdm import tqdm

# Enable TQDM for pandas operations
tqdm.pandas()

# Use progress_apply instead of apply
df = pd.DataFrame({'data': range(1000)})
result = df['data'].progress_apply(lambda x: x**2)

Multiple Bars with Nesting

Create nested progress bars for hierarchical operations:

from tqdm import tqdm
import time

for i in tqdm(range(10), desc="Outer loop"):
    for j in tqdm(range(100), desc="Inner loop", leave=False):
        time.sleep(0.001)  # Simulate work

TQDM in Different Environments

Command Line

The default TQDM interface works in any terminal:

from tqdm import tqdm
for i in tqdm(range(100)):
    pass  # Your operation here

Jupyter Notebooks

For Jupyter notebooks, use tqdm.notebook:

from tqdm.notebook import tqdm
for i in tqdm(range(100)):
    pass  # Your operation here

GUIs and Web Applications

For GUI applications or when you want to redirect output:

from tqdm.gui import tqdm
# or
from tqdm import tqdm
bar = tqdm(range(100), file=open('progress.log', 'w'))

Performance Considerations

TQDM is designed to be lightweight, but to maximize performance:

  1. Batch Updates: For very fast iterations, update the bar less frequently
    with tqdm(total=1000000) as pbar:
        for i in range(0, 1000000, 100):
            # Do 100 iterations
            pbar.update(100)  # Update once per 100 iterations
    
  2. Disable When Not Needed: Turn off progress bars in production environments
    from tqdm import tqdm
    is_debug = True  # Set based on environment
    for i in tqdm(range(100), disable=not is_debug):
        pass  # Your code here
    

TQDM for Parallel Processing

With Multiprocessing

from tqdm import tqdm
from multiprocessing import Pool

def process(item):
    # Process a single item
    return item * 2

with Pool(4) as p:
    results = list(tqdm(p.imap(process, range(100)), total=100))

With Concurrent Futures

from tqdm import tqdm
from concurrent.futures import ProcessPoolExecutor
import time

def process(item):
    time.sleep(0.01)  # Simulate work
    return item * 2

with ProcessPoolExecutor(max_workers=4) as executor:
    results = list(tqdm(executor.map(process, range(100)), total=100))

Customizing TQDM

Color and Format

from tqdm import tqdm
import time

# Custom format for the progress bar
for i in tqdm(range(100), 
              bar_format="{l_bar}{bar:30}{r_bar}{bar:-30b}",
              colour="green"):
    time.sleep(0.01)

Custom Metrics

from tqdm import tqdm
import time
import random

pbar = tqdm(range(100))
for i in pbar:
    # Simulate variable processing time
    processing_time = random.uniform(0.01, 0.1)
    time.sleep(processing_time)
    
    # Display custom metrics (e.g., processing time)
    pbar.set_postfix(time=f"{processing_time:.3f}s", 
                     throughput=f"{1/processing_time:.2f} items/s")

Real-World Applications

Machine Learning Training

from tqdm import tqdm
import numpy as np

# Simulate training epochs
epochs = 20
for epoch in tqdm(range(epochs), desc="Training"):
    # Simulate batch processing
    batches = 100
    losses = []
    for batch in tqdm(range(batches), desc=f"Epoch {epoch+1}/{epochs}", leave=False):
        # Simulate training step
        loss = 1.0 - 0.005 * (epoch + batch/batches)
        losses.append(loss)
        
    # Update epoch progress bar with mean loss
    mean_loss = np.mean(losses)
    tqdm.write(f"Epoch {epoch+1}/{epochs}, Loss: {mean_loss:.4f}")

Data Processing Pipeline

from tqdm import tqdm
import time
import random

# Simulate a data processing pipeline
def load_data(files):
    results = []
    for file in tqdm(files, desc="Loading data"):
        time.sleep(0.02)  # Simulate loading
        results.append({"file": file, "data": random.random()})
    return results

def process_data(items):
    results = []
    for item in tqdm(items, desc="Processing"):
        time.sleep(0.05)  # Simulate processing
        results.append({"processed": item["data"] * 2})
    return results

def save_results(results):
    for i, result in enumerate(tqdm(results, desc="Saving")):
        time.sleep(0.01)  # Simulate saving
        
# Execute pipeline
files = [f"file_{i}.txt" for i in range(50)]
data = load_data(files)
processed = process_data(data)
save_results(processed)

Best Practices

  1. Keep It Simple: For most cases, the basic tqdm(iterable) syntax is sufficient
  2. Set Total: Always specify the total parameter when using manual updates
  3. Close When Done: Always close manually created progress bars with .close()
  4. Use Descriptive Labels: Add context with the desc parameter
  5. Nested Progress Bars: Use leave=False for inner loops to avoid cluttering
  6. Log Messages: Use tqdm.write() instead of print() to avoid breaking progress bars
  7. Unit Awareness: Set appropriate unit and unit_scale for better readability

To summarize

TQDM has become an indispensable tool in the Python ecosystem for a reason: it combines simplicity with powerful functionality. By providing clear visual feedback on long-running operations, it improves the developer experience and helps users understand what's happening behind the scenes.

Whether you're processing large datasets, training machine learning models, or running any time-consuming operation, TQDM offers an elegant solution for progress tracking with minimal overhead. Its flexibility across different environments and extensive customization options make it suitable for virtually any Python project.

Similar Articles

https://www.geeksforgeeks.org/python-how-to-make-a-terminal-progress-bar-using-tqdm/

https://pypi.org/project/tqdm/

More Articles from Python Central

Python Tabulate: Creating Beautiful Tables from Your Data

Data Visualization with Python: Making Data Look Good Enough to Frame