I've got a python dict with where each key corresponds to a heading, and the list associated with each heading contains an arbitrary number of values:
data = {
"heading1": ['h1-val1', 'h1-val2', 'h1-val3', ],
"heading2": ['h2-val1', ],
"heading3": ['h3-val1', 'h3-val2', 'h3-val3', 'h3-val4', ],
}
I need to render this in a Django template as a table, where the values are listed vertically beneath each heading, with any missing values rendered as an empty table cell:
<table>
<thead>
<tr>
<th>heading1</th>
<th>heading2</th>
<th>heading3</th>
</tr>
</thead>
<tbody>
<tr>
<td>h1-val1</td>
<td>h2-val1</td>
<td>h3-val1</td>
</tr>
<tr>
<td>h1-val2</td>
<td></td>
<td>h3-val2</td>
</tr>
<tr>
<td>h1-val3</td>
<td></td>
<td>h3-val3</td>
</tr>
<tr>
<td></td>
<td></td>
<td>h3-val4</td>
</tr>
</tbody>
</table>
What's the best way to achieve this?
My first inclination is to rearrange the original dict into a 2D matrix, and just pass that into the template. I'm sure I'm not the first to run into this kind of problem, though, and I'm curious how others have solved this problem.
UPDATE: Just for reference, here's my original solution to this problem (which I'm not very happy with).
# Using the data dict from the question:
size = max(len(data['heading1']), len(data['heading2']), len(data['heading3']))
matrix = [[None, None, None] for i in range(size)] # initialize an empty matrix
# manually copy the data into the appropriate column :(
i = 0
for item in data['heading1']:
matrix[i][0] = item
i += 1
i = 0
for item in data['heading2']:
matrix[i][1] = item
i += 1
i = 0
for item in data['heading3']:
matrix[i][2] = item
i += 1
I then passed the matrix into the template which looked like this:
<table>
<thead><tr>
<th>heading1</th>
<th>heading2</th>
<th>heading3</th>
</tr></thead>
<tbody>
{% for row in matrix %}
<tr>
{% for col in row %}
<td>{% if col %}{{ col }}{% else %} {% endif %}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>