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>You could use an example supplied in the docs for <a href="https://docs.python.org/3/library/contextlib.html" rel="nofollow"><code>contextlib</code></a> and build it accordingly.</p>

<p>Essentially, create a context manager with <a href="https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager" rel="nofollow"><code>@contextmanager</code></a> that adds initial and closing tags and print out the required values in the <code>with</code> block. The context manager, as found in the docs:</p>

<pre><code>from contextlib import contextmanager

@contextmanager
def tag(name):
    print("&lt;%s&gt;" % name, end='')
    yield
    print("&lt;/%s&gt;" % name, end='')
</code></pre>

<p>and, the actual sample that uses it:</p>

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

for d in l:
    with tag('tr'):
        with tag('td'):
            print(d['username'], end='')
        with tag('td'):
            print(d['password'], end='')
    print()
</code></pre>

<p>Outputs:</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>

python python-3.x