Iterate on multiple list in single for loop in Jinja

Hi,
I need to iterate on multiple lists in a single loop,
here is what i’ve tried but its giving me the error

Tried :
toomanyvalues
Error :
ValueError : Too many values to unpack

Anybody??

Would zip work?

I’ve already tried
for k,w,m in zip(kpi, weightage, marks_obtained)
but its giving error “Zip not found” something like this.

I tried this work arround and it’s working fine for me.
May be it’ll work for you also.

list_1  = [1, 2,3,4]
list_2 = [1,2,3,4]
<table>
{% for i in list_1 %}
      {% set item_1 = list_1[loop.index-1] %}
      {% set item_2 = list_2[loop.index-1] %}
     <tr>
           <td> {{ item_1 }} </td>
           <td>  {{ item_2 }} </td>
    </tr>
{% endfor %}
  </table>

loop.index always start with index 1

NOTE:
Your all lists len must be balance or you can handle the condition for exception index out of range

3 Likes

@navdeepghai Thank you so much. :slight_smile:

it works, thanks!

1 Like

I know this is old but thanks for this. Really helped me out

There is a problem with this solution. if list 2 length is not equal to list 1 then it’ll error out.
You can do it like this
list_length = what ever is the smaller length out of list_1 and list2

loop through the list_length and then use it to index the lists.