views:

311

answers:

1

hi,

i've got an appengine project and in my template i want to do something like

{% for i in range(0, len(somelist)) %}
  {{ somelist[i] }} {{ otherlist[i] }}
{% endfor %}

i've tried using 'forloop.counter' to access list items too, but that didn't work out either. any suggestions?

regards, mux

+5  A: 

What you might want to do instead is change the data that you're passing in to the template so that somelist and otherlist are zipped together into a single list:

combined_list = zip(somelist, otherlist)
...
{% for item in combined_list %}
    {{ item.0 }} {{ item.1 }}
{% endfor %}
Miles
This is the right idea, although the syntax for list item lookup in Django templates is {{ item.0 }} {{ item.1 }}
Daniel Roseman
Thanks, I forgot. Fixed...
Miles
thank you for the tip! i read that 'zip' can have more than two lists as input, so as long as i match up everything correctly beforehand that's exactly what i was looking for.thank you!
mux
You may consider using itertools.izip which returns a generator instead of a complete list.
DasIch