'Flask: Iterating through a dictionary whose vlaues are lists of tuples
I have a dict whose values are lists of tuples. I want to build a table for every key.
mydict = {'Western Division': [(0, 1, 'Oakland'), (0, 2, 'San Jose')], 'Eastern Division': [(1, 1, 'Boston'), (1, 2, 'Buffalo')]}
My template is:
{% for key, value in mydict %}
<table>
<tr>
<th> {{ key }} </th>
</tr>
{% for team in value %}
<tr>
<td>{{ team[2] }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}
This gives me a ValueError: too many values to unpack (expected 2)
I tried changing the first for-loop to for key, value, team, thinking that I want to call each tuple in each list in each key, but got the same error (expected 3).
Lastly, I tried for key, value in mydict.items and got TypeError: 'builtin_function-or_method' object is not iterable.
It's definitely possible that I made a mistake further upstream in creating the dict, but I suspect I am just not building my template correctly.
Solution 1:[1]
@mechanical_meat provided the answer in their comment. items needs parens in jinja2.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JayCo741 |
