0

In a Django template, I want to access the elements in one list, whilst looping over elements in another list. So, I pass the lists mylist1 and mylist2 (both have the same number of elements) to the template as context variables, and I want to loop through mylist1, display the element, and then also display the corresponding element of mylist2. It would look something like this:

{% for result in mylist1 %}
    <p>result</p>
    <p>{{ mylist2.forloop.counter }}</p>
{% endfor %}

What's the correct solution?

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
  • Which is the relation between `mylist1` and `mylist2`? – cor Oct 28 '14 at 16:23
  • They have the same number of elements. I realise that I could probably zip them and pass them as the same variable, but I'm wondering if there's a solution to the specific problem above. – Karnivaurus Oct 28 '14 at 16:25
  • So you just want to display the number of elements of `mylist2`? – cor Oct 28 '14 at 16:26
  • No, I want to display the corresponding element. So, I want to display `mylist1[0]` and `mylist2[0]`, then `mylist1[1]` and `mylist2[1]`, and so on ... – Karnivaurus Oct 28 '14 at 16:29
  • 2
    http://stackoverflow.com/questions/2415865/iterating-through-two-lists-in-django-templates – cor Oct 28 '14 at 16:32

0 Answers0