Building HTML table from list of dictionaries
<p>I have a list containing dictionaries that I would like to output into an HTML table.</p>

<p>My list looks like this:</p>

<pre><code>[{'description': 'KA8ts5', 'password': 'KA8ts5', 'username': 'test4'}, 
 {'description': '5j6mEF', 'password': '5j6mEF', 'username': 'test5'}]
</code></pre>

<p>I am trying to make it look as follows:</p>

<pre><code>&lt;tr&gt;&lt;td&gt;test4&lt;/td&gt;&lt;td&gt;KA8ts5&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;test5&lt;/td&gt;&lt;td&gt;5j6mEF&lt;/td&gt;&lt;/tr&gt;
</code></pre>

<p>But I am unsure how to get these values as well as format them accordingly.</p>

<p>I would use a <em>template engine</em> like <a href="http://www.makotemplates.org/" rel="nofollow"><code>mako</code></a> or <a href="http://jinja.pocoo.org/docs/dev/" rel="nofollow"><code>jinja2</code></a>.</p>

<p>Example using <code>mako</code>:</p>

<pre><code>from mako.template import Template

template = """
&lt;table&gt;
% for user in users:
     &lt;tr&gt;
        &lt;td&gt;${user['username']}&lt;/td&gt;
        &lt;td&gt;${user['description']}&lt;/td&gt;
     &lt;/tr&gt;
% endfor
&lt;/table&gt;
"""

users = [
    {'description': 'KA8ts5', 'password': 'KA8ts5', 'username': 'test4'},
    {'description': '5j6mEF', 'password': '5j6mEF', 'username': 'test5'}
]
result = Template(template).render(users=users)
print(result)
</code></pre>

<p>Prints:</p>

<pre><code>&lt;table&gt;
     &lt;tr&gt;
        &lt;td&gt;test4&lt;/td&gt;
        &lt;td&gt;KA8ts5&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
        &lt;td&gt;test5&lt;/td&gt;
        &lt;td&gt;5j6mEF&lt;/td&gt;
     &lt;/tr&gt;
&lt;/table&gt;
</code></pre>

python python-3.x