1

I have a list of dictionary items I am looping over.
I can get the key which is the year, when I try to get the data in the dictionary it's printing the year again.

  [{'2020': [], '2019': ['05'], '2018': ['02', '01']}] 
  {% for d in  dirs %}
      {% for sd in d %}
     {{ sd }}
         {% for doy in sd %}   
            {{ doy }}<br>
         {% endfor %}<br><br>
    {% endfor %}
{% endfor %}

Whats is printing is

 2020
 2
 0
 2
 0

2019
2
0
1
9

2018
2
0
1
8

What I want to print is

2020
2019
05
2018
02
01
user3525290
  • 1,557
  • 2
  • 20
  • 47

2 Answers2

2

Change

{% for doy in sd %}

to

{% for doy in d[sd] %}

The reasoning here is that iterating over a dictionary using the for ... in ... syntax iterates over the keys of the dictionary. That means that your nested loop was then attempting to loop over the values within the key of the dictionary rather than the value of the key of the dictionary.

Chathan Driehuys
  • 1,173
  • 3
  • 14
  • 28
1

As mentioned above, you can achieve that by modifying your third loop.
To simplify:

list = [{'2020': [''], '2019': ['05'], '2018': ['02', '01']}]

Template:

{% for obj in list %}
  {% for obj2 in obj %}
    <p>{{ obj2 }}</p>
    {% for obj3 in obj[obj2] %}
     <p>{{ obj3 }}</p>
    {% endfor %}
  {% endfor %}
{% endfor %}

Output:

2020   
2019   
05   
2018  
02   
01
Gabe
  • 956
  • 7
  • 8