GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Tracking Progress in Python with tqdm - Complete Guide Tracking Progress in Python with tqdmlast modified March 31, 2025 The tqdm library in Python provides an easy way to add progress bars to your loops and tasks. This tutorial covers its installation, basic usage, and advanced features with practical examples for various scenarios. Tracking progress is essential for long-running operations, whether you're processing files, training models, or iterating over large datasets. We'll explore how tqdm simplifies this with minimal code changes. Installation and Basic SetupFirst, install tqdm using pip and import it in your Python script. tqdm_install.py
# Install via pip
# pip install tqdm
from tqdm import tqdm
import time
# Basic loop with progress bar
for i in tqdm(range(100)):
time.sleep(0.1) # Simulate work
To use tqdm, install it with Customizing the Progress BarYou can customize tqdm's appearance and behavior with parameters. tqdm_custom.py
from tqdm import tqdm
import time
# Custom description and bar format
for i in tqdm(range(100), desc="Processing", bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}"):
time.sleep(0.05)
The Tracking File ProcessingUse tqdm to monitor file operations like reading lines. tqdm_file.py
from tqdm import tqdm
# Process a file line by line
with open('large_file.txt', 'r') as f:
total_lines = sum(1 for _ in f) # Count lines
f.seek(0) # Reset file pointer
for line in tqdm(f, total=total_lines, desc="Reading file"):
pass # Process line here
For file operations, tqdm can track progress by setting the Nested Loops with tqdmHandle multiple progress bars for nested iterations. tqdm_nested.py
from tqdm import tqdm
import time
# Nested loops with separate bars
for i in tqdm(range(5), desc="Outer"):
for j in tqdm(range(10), desc="Inner", leave=False):
time.sleep(0.1)
Tqdm supports nested loops by creating multiple bars. The Manual Progress UpdatesUpdate progress manually for non-iterable tasks. tqdm_manual.py
from tqdm import tqdm
import time
# Manual control of progress
with tqdm(total=100, desc="Manual Task") as pbar:
for i in range(10):
time.sleep(0.2)
pbar.update(10) # Update by 10 units
For tasks without natural iterables, use tqdm as a context manager. The
Progress with List Comprehension
Track progress in list comprehensions using tqdm_listcomp.py
from tqdm import tqdm import time # List comprehension with progress result = [x * 2 for x in tqdm(range(100), desc="Computing")] time.sleep(0.1) # Simulate work after
Wrap the iterable in a list comprehension with Integration with PandasUse tqdm with Pandas for data processing tasks. tqdm_pandas.py
import pandas as pd
from tqdm import tqdm
# Enable tqdm for pandas
tqdm.pandas()
# Process DataFrame rows
df = pd.DataFrame({'a': range(1000)})
df['b'] = df['a'].progress_apply(lambda x: x * 2)
Tqdm integrates with Pandas via Best Practices
Source ReferencesAuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |