Why are the values of the dict in the list is not printing out?

59 Views Asked by At

So I want to loop across several list each hasving one or multiple dictionaries. For Example given:

    r = [{"symbol":10},{"symbol":15},{"symbol":25}]
    h = [{"sy":15},{"sy":23},{"sk":64}]
    i = [{"sl":45},{"sl":67},{"sl":98}]

I want it to print:

    Symbol sy sl
    10      15  45
    15      23  67
    25      64  98

I did it in python and it worked perfectly:

    for p in r,h,i:
        if p == r:
            print(p[c]["symbol"])
        elif p == h:
            print(p[c]["sy"])
        elif p == i:
            print(p[c]["sl"])

it works perfectly in python.But when outputting in jinja only the first word is outputted.I'm using flask to communicate with the server side which is written in python. But I am having some issuees doing it in jinja: Here my jinja code:

    
    {%set c = 0%}
    {% for s in symbol,stockname,shares,price,total%}
    <tr>
        {%if s == symbol%}
          <td> {{s[c].symbol}}</td>

        {%if s == stockname %}
          <td> {{s[c].stockname}}</td>

        {%if s == shares %}
          <td> {{s[c].shares}}</td>

        {%if s == price %}
          <td> {{s[c].price}}</td>


        {%if s == total %}
          <td> {{s[c]["total"]}}</td>
    </tr>
        {%set c = c + 1%}

        {%endif%}
        {%endif%}
        {%endif%}
        {%endif%}
        {%endif%}

    {%endfor%}


    </table>
    {% endblock %}```
1

There are 1 best solutions below

0
On

I figured it out. First I must say that I wasn't getting the input I wanted in python. In python to get the each list of dictionaries output how I wanted I had to do take this approach:

r = [{"symbol":10},{"symbol":15},{"symbol":25}]
h = [{"sy":15},{"sy":23},{"sy":64}]
i = [{"sl":45},{"sl":67},{"sl":98}]
c = 0
n = []
for p in r,h,i:
    print(r[c]["symbol"])
    print(h[c]["sy"])
    print(i[c]["sl"])
    c+=1