How to output loop.counter in python jinja template?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Jinja templates, the value people often look for as loop.counter is actually exposed under a different name. Jinja uses loop.index for a one-based counter and loop.index0 for a zero-based counter, so the fix is mostly about using the correct loop variable.
Use loop.index Instead of loop.counter
Inside a Jinja for loop, loop.index gives you the current iteration number starting from 1.
If names is ["Ana", "Bob", "Cara"], the rendered output becomes:
That is the direct Jinja equivalent of the loop counter many developers expect from other template engines.
Use loop.index0 for Zero-Based Counting
If you need a counter that starts at 0, use loop.index0.
This is useful when the template output has to match zero-based array indexes or JavaScript code.
Jinja also provides other loop helpers:
- '
loop.first' - '
loop.last' - '
loop.revindex' - '
loop.revindex0' - '
loop.length'
Those are often more expressive than manually calculating conditions from the counter.
Full Example with Flask
Here is a minimal Flask app that passes data to a template and renders the loop index correctly.
When you open the page, the first column contains 1, 2, and 3.
Nested Loops Need Care
In nested loops, loop always refers to the current loop. If you need the outer loop later, save it to another template variable before entering the inner loop.
Without that extra assignment, the inner loop would overwrite your reference to the outer one.
Avoid Computing Counters in Python When Jinja Already Has Them
Sometimes people pre-enumerate data in Python just to get a display counter:
That works, but it is often unnecessary when the template is already looping over the items. Using Jinja's built-in loop metadata keeps the view code simpler:
Let the template handle presentation counters unless the index itself is part of the business data.
Common Pitfalls
The biggest mistake is assuming Jinja has a loop.counter variable because another template engine uses that name. Another common issue is confusing loop.index with loop.index0, which causes off-by-one numbering in the rendered output. Developers also run into trouble with nested loops when they expect loop to keep pointing at the outer iteration. Finally, some code enumerates data in Python and then also uses loop.index in the template, which creates redundant counters and harder-to-read templates.
Summary
- Jinja does not use
loop.counter; useloop.indexfor one-based counting. - Use
loop.index0when you need zero-based counting. - Jinja loop objects also expose helpers such as
loop.firstandloop.last. - Save the outer
loopto another variable if you need it inside nested loops. - Prefer Jinja's built-in loop metadata over extra counter preparation in Python when possible.

