Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6203955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:00:23+00:00 2026-05-24T05:00:23+00:00

I’ve got a python dict with where each key corresponds to a heading, and

  • 0

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 %}&nbsp;{% endif %}</td>
    {% endfor %}
    </tr>
{% endfor %}
</tbody>
</table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:00:24+00:00Added an answer on May 24, 2026 at 5:00 am

    If we change the game a little bit, it’s actually a snap to turn this around (so long as your lists are None filled…)

    from django.template import Context, Template
    
    data = {
        "heading1": ['h1-val1', 'h1-val2', 'h1-val3', ],
        "heading2": ['h2-val1', ],
        "heading3": ['h3-val1', 'h3-val2', 'h3-val3', 'h3-val4', ],
    }
    
    # we'll need to split the headings from the data
    # rather than using keys() I'm just hard coding so I can control the order
    headings = ["heading1", "heading2", "heading3"]
    
    columns = [data[heading] for heading in headings]
    
    # get the length of the longest column
    max_len = len(max(columns, key=len))
    
    for col in columns:
        # padding the short columns with None
        col += [None,] * (max_len - len(col))
    
    # Then rotate the structure...
    rows = [[col[i] for col in columns] for i in range(max_len)]
    
    
    dj_template ="""
    <table>
    {# headings #}
        <tr>
        {% for heading in headings %}
            <th>{{ heading }}</th>
        {% endfor %}
        </tr>
    {# data #}
    {% for row in data %}
        <tr>
            {% for val in row %}
            <td>{{ val|default:'' }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
    </table>
    """
    
    # finally, the code I used to render the template:
    tmpl = Template(dj_template)
    tmpl.render(Context(dict(data=rows, headings=headings)))
    

    For me, this produces the following (blank lines stripped):

    <table>
        <tr>
            <th>heading1</th>
            <th>heading2</th>
            <th>heading3</th>
        </tr>
        <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>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i got an object with contents of html markup in it, for example: string
I want to construct a data frame in an Rcpp function, but when I
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.