0

I have a 2D List as follows:

my_list = [["2","","4"],["3","",""],["1","1",""]]

In Python, we can iterate over the above-mentioned list if we want to know the index of items in the following way.

   for row in range(len(my_list)):
       for column in range(len(my_list[row])):
           print("Item at position ", row, column)
           print(my_list[row][column)

I am trying to implement the above approach in Django Template as well. I can access the element directly in the following manner:

for row in my_list

The above approach works perfectly, but I need to access index and that's why I need Nested Numeric Loops:

I tried the following approach given in this link: Numeric Loops
I made a solution mentioned below but it is not printing anything

{% for row in '0123' %}
    {% for column in my_list.row %}
        <p> {{my_list.row.column}}</p>
    {% endfor %}
{% endfor %}

What am I doing wrong in the above code & is there any simpler approach to this?

skaul05
  • 2,154
  • 3
  • 16
  • 26

1 Answers1

2

Looks like you need forloop.parentloop & forloop.counter.

{% for row in my_list %}
    {% for column in row %}
        <p> {{forloop.parentloop.counter}} {{ forloop.counter }} </p>
        <p> {{ column }} </p>
    {% endfor %}
{% endfor %}

MoreInfo

Rakesh
  • 81,458
  • 17
  • 76
  • 113