0

I am trying to loop through a multi level list in python. The list I have is like this:

data =[{'incircle': [797, 786], 'rid': 1, 'shots': 1000, 'pi_values': [3.188, 3.144], 'average_pi': 3.1660000000000004}, {'incircle': [805, 799], 'rid': 2, 'shots': 1000, 'pi_values': [3.22, 3.196], 'average_pi': 3.208}, {'incircle': [766, 779], 'rid': 3, 'shots': 1000, 'pi_values': [3.064, 3.116], 'average_pi': 3.09}, {'incircle': [772, 791], 'rid': 4, 'shots': 1000, 'pi_values': [3.088, 3.164], 'average_pi': 3.1260000000000003}, {'incircle': [798, 808], 'rid': 5, 'shots': 1000, 'pi_values': [3.192, 3.232], 'average_pi': 3.212}, {'incircle': [785, 791], 'rid': 6, 'shots': 1000, 'pi_values': [3.14, 3.164], 'average_pi': 3.152}]

I am trying to display incircle, rid, shots & pi_values inside a html table using python flask. Id and shots will be all same for each list incircle value. Here is what I am trying:

        <table>
    <tr>
                <td>ID</td>
                <td>Incircle Value</td>
                <td>Shots</td>
                <td>PI Value</td>
        </tr>     
{%for obj in data%}
<tr>
                <td>{{data['rid']}}</td>
        {%for value in obj['incircle'] %}
                <td>{{ value }}</td>
        {%endfor%}
                <td>{{data['shots']}}</td>
        {%for value in obj['pi_values'] %}
                <td>{{ value }}</td>
        {%endfor%}
</tr>
{%endfor%}
  </table>

So I am trying to display the list into a table like this.

Sample Table that I am trying to achive

1 Answers1

0

I think something like this should do the trick:

<table>
    <tr>
        <td>ID</td>
        <td>incircle</td>
        <td>shots</td>
        <td>pi_value</td>
    </tr>
    {% for obj in data %}
        {% for incircle, pi_value in zip(obj['incircle'], obj['pi_values']) %}
            <tr>
                <td>{{ obj['rid'] }}</td>
                <td>{{ incircle }}</td>
                <td>{{ obj['shots'] }}</td>
                <td>{{ pi_value }}</td>
            </tr>
        {% endfor %}
    {% endfor %}
</table>

You might have some problems with the zip function, you could just pass it as a parameter to the page (more here: zip(list1, list2) in Jinja2?)

lennertcl
  • 93
  • 6