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?

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.