Jinja - Index number of elements in a list

by
Jeremy Canfield |
Updated: June 22 2024
| Jinja articles
Here is a basic example of how to create and print a list in Jinja.
{% set fruits=['apple','banana','grape','orange'] %}
{{ print fruits }}
loop.index can be used to return the index number of each element in a loop.
{% set fruits=['apple','banana','grape','orange'] %}
{% for fruit in fruits %}
{{ fruit }} index is {{ loop.index }}
{% endfor %}
Or, you can print a specific index item from the list. In this example, index item 0 (apple) will be printed.
{% set fruits=['apple','banana','grape','orange'] %}
{% for fruit in fruits %}
{{ fruit[0] }}
{% endfor %}
Did you find this article helpful?
If so, consider buying me a coffee over at