Wrap any iterable with tqdm(iterable) from the tqdm library (pip install tqdm) for an instant, zero-configuration terminal/Jupyter progress bar with live ETA and iteration speed, rich.progress.track(iterable) when you want styled multi-column progress bars, or a pure-Python sys.stdout.write("\r...") helper using carriage return \r and flush=True when zero external dependencies are permitted.

Basic Example

import sys
import time

# 1. Recommended (Third-party `tqdm` — `pip install tqdm`):
from tqdm import tqdm

for item in tqdm(range(50), desc="Processing items", unit="item"):
    time.sleep(0.02)
# Output (live in-place bar):
# Processing items: 100%|████████████████████| 50/50 [00:01<00:00, 48.50item/s]

# 2. Pure Python (Zero dependencies — carriage return `\r` + `flush=True`):
def simple_progress(iterable, total: int, width: int = 30):
    for i, item in enumerate(iterable, start=1):
        yield item
        filled = int(width * i / total)
        bar = "█" * filled + "-" * (width - filled)
        sys.stdout.write(f"\rProgress: |{bar}| {i}/{total} ({i / total:.0%})")
        sys.stdout.flush()
    sys.stdout.write("\n")

for _ in simple_progress(range(50), total=50):
    time.sleep(0.02)

Key Details & Edge Cases

  • Never call print() directly inside a tqdm loop (tqdm.write() instead):
    • Standard print("msg") prints a newline that splits the progress bar into dozens of stacked lines. Call tqdm.write("log message") instead, which temporarily clears the bar, prints your message cleanly above it, and redraws the bar underneath.
  • Generators and iterators with unknown length (total=...):
    • If you wrap a generator or database cursor without __len__, tqdm(gen) displays an iteration counter and speed (1250it [00:02, 610it/s]) without a percentage bar. Pass total=expected_count (tqdm(gen, total=10_000)) to restore the percentage bar and ETA.
  • Jupyter Notebook / JupyterLab rendering (from tqdm.auto import tqdm):
    • Import from tqdm.auto import tqdm instead of from tqdm import tqdm so tqdm automatically renders rich HTML/widget bars inside Jupyter notebooks and clean ASCII/Unicode bars in terminals.
  • Pandas .apply() progress bars (tqdm.pandas()):
    • Register Pandas integration once via tqdm.pandas(desc="Transforming") and replace .apply(...) / .map(...) with .progress_apply(...) / .progress_map(...).
  • Avoid slowing down tight loops with millions of fast iterations (mininterval):
    • Updating stdout on every iteration of a 10-million-step CPU loop adds terminal I/O overhead. tqdm automatically throttles redraws to every 0.1s (mininterval=0.1), whereas a naive pure-Python sys.stdout.write("\r...") without throttling (if i % 1000 == 0 or i == total:) will bottleneck tight loops.

Comparison of Progress Bar Approaches in Python

Method External Dependency? Shows ETA, Rate (it/s) & Throttling? Works in Jupyter Notebooks? Best Used When
tqdm.auto.tqdm(iterable) (Recommended) Yes (pip install tqdm) Yes (auto-throttled, nested bars, Pandas support) Yes (HTML widget) Standard CLI scripts, data pipelines, ML training loops, and Jupyter notebooks.
rich.progress.track(iterable) Yes (pip install rich) Yes (live spinner, elapsed/remaining time, colors) Yes Modern CLI applications requiring multi-task progress bars alongside styled terminal output.
Pure Python \r + sys.stdout.flush() No (built-in sys) No (manual calculation & throttling needed) Partial (works in basic output cells) Environments where zero third-party packages can be installed.