how to iterate over a list of list in jinja

Jinja2

Jinja2 Problem Overview


I have a list of list like :

    [[elem0, elem1, elem2], [elem3, elem4, elem5], [elem6, elem7, elem8], ...]

I wrote the follow template file :

    {% for result in results %}
        <tr>
            <td>result[0]</td>
            <td>result[1]</td>
            <td>result[2]</td>
        </tr>
    {% endfor %}

But it didn't work, What i can think is use nested for. Is there another method to access the element in the list in jinja?

Jinja2 Solutions


Solution 1 - Jinja2

You still need to output the loop variables inside braces.

{% for result in results %}
    <tr>
        <td>{{ result[0] }}</td>
        <td>{{ result[1] }}</td>
        <td>{{ result[2] }}</td>
    </tr>
{% endfor %}

Also, consider a nested for loop:

{% for result in results %}
    <tr>
    {% for elem in result %}
        <td>{{elem}}</td>
    {% endfor %}
    </tr>
{% endfor %}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstamaimerView Question on Stackoverflow
Solution 1 - Jinja2zxzakView Answer on Stackoverflow