PyGuide

Learn Python with practical tutorials and code examples

Code Snippet Beginner
• Updated Jul 10, 2025

Python For Loop with Counter: Ready-to-Use Code Examples

Copy-paste Python for loop with counter code snippets for common tasks like progress tracking, numbering, and data processing.

Python For Loop with Counter: Ready-to-Use Code Examples

These Python for loop with counter code snippets provide ready-to-use solutions for common programming tasks. Copy and customize these examples to add counters to your for loops efficiently.

Basic Counter Snippets #

Simple Numbered List #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Create numbered lists for menus, instructions, or ordered displays.

Progress Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Display progress for file processing, data analysis, or batch operations.

Index-Based Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: When you need both index and value for array-like operations.

Advanced Counter Patterns #

Conditional Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Count specific types of items while processing a sequence.

Multiple Counters #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Categorize and count items simultaneously.

Step Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Process items in groups or batches with counter tracking.

File and Data Processing #

File Processing Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Track file processing operations with detailed status.

Data Analysis Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Analyze sequential data with trend indicators.

Row Processing Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Process CSV-like data or database records with row tracking.

User Interface Helpers #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Create numbered menus for command-line interfaces.

Form Field Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Validate form data with field counting and status tracking.

Loop Control Patterns #

Break with Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Search operations with attempt counting.

Continue with Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Skip invalid items while tracking processing count.

Nested Loop Counters #

Matrix Processing #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Process spreadsheet-like data or 2D arrays.

Grouped Processing #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Process categorized data with hierarchical numbering.

Performance Optimized Snippets #

Generator with Counter #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Process large datasets with memory-efficient counting.

Efficient Index Access #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Usage: Efficient processing when you need current and next values.

Customization Tips #

Custom Start Values #

# Different start values for different contexts
for i, item in enumerate(items, start=1):    # User-friendly (1, 2, 3...)
for i, item in enumerate(items, start=0):    # Programming (0, 1, 2...)
for i, item in enumerate(items, start=10):   # Custom numbering (10, 11, 12...)

Format Strings #

# Various formatting options
print(f"{counter:2d}. {item}")           # Right-aligned numbers
print(f"{counter:03d}: {item}")          # Zero-padded numbers
print(f"[{counter:4d}] {item}")          # Bracketed numbers
print(f"Item #{counter}: {item}")        # Hash-prefixed numbers

Counter Conditions #

# Conditional counter logic
counter = 0
for item in items:
    if condition(item):
        counter += 1
        print(f"Match #{counter}: {item}")

Quick Reference #

PatternUse CaseCode
Basic numberingUser-friendly listsenumerate(items, start=1)
Index accessArray operationsrange(len(items))
Progress trackingLong operations(i/total)*100
Conditional countingFiltered processingManual increment
Multiple countersCategorizationSeparate variables
Nested counting2D dataNested enumerate

Copy these snippets and modify them for your specific use cases. All examples follow Python best practices and are optimized for readability and performance.

For more advanced loop techniques, check out our Python iteration patterns and performance optimization guide.

Related Snippets

Snippet Beginner

Python For Loop Counter Code Examples

Ready-to-use Python code snippets for implementing for loops with counters using enumerate(), range(), and custom counter patterns.

#python #for-loop #counter +2
View Code
Syntax
Snippet Beginner

Python For Loop Break: Ready-to-Use Code Examples

Copy-paste Python for loop break code snippets for search operations, early termination, and loop control in various scenarios.

#python #for-loop #break +2
View Code
Syntax
Snippet Beginner

Python Enumerate Start at 1: Code Examples and Snippets

Ready-to-use Python enumerate start at 1 code snippets for menus, lists, rankings, and user-friendly numbering systems.

#python #enumerate #start +2
View Code
Syntax
Snippet Intermediate

Python Enumerate Zip: Ready-to-Use Code Examples

Copy-paste Python enumerate zip code snippets for combining multiple sequences with index tracking and parallel iteration.

#python #enumerate #zip +2
View Code
Syntax