print a list of dictionaries in table form
<p>assume I have this list as global list </p>

<pre><code> dataset =[{'Major': 'Biology', 'GPA': '2.4', 'Name': 'Edward'},
           {'Major': 'Physics', 'GPA': '2.9', 'Name':'Emily'},  
           {'Major':'Mathematics', 'GPA': '3.5', 'Name': 'Sarah'}]
</code></pre>

<p>and a want a function print() to print it as </p>

<pre><code> name     major        GPA 
 =============================== 
 edward   Biology      2.4 
 Emily    physics      2.9 
 sarah    mathematics  3.5
</code></pre>

<p>you can use module <a href="https://pypi.python.org/pypi/tabulate" rel="nofollow">tabulate</a></p>

<pre><code>&gt;&gt;&gt; import tabulate
&gt;&gt;&gt; dataset =[{'Major': 'Biology', 'GPA': '2.4', 'Name': 'Edward'}, {'Major': 'Physics', 'GPA': '2.9', 'Name': 'Emily'}, {'Major': 'Mathematics', 'GPA': '3.5', 'Name': 'Sarah'}]
&gt;&gt;&gt; header = dataset[0].keys()
&gt;&gt;&gt; rows =  [x.values() for x in dataset]
&gt;&gt;&gt; print tabulate.tabulate(rows, header)
Major          GPA  Name
-----------  -----  ------
Biology        2.4  Edward
Physics        2.9  Emily
Mathematics    3.5  Sarah
</code></pre>

<p>you can use <code>tablefmt</code> parameter for different table format</p>

<pre><code>&gt;&gt;&gt; print tabulate.tabulate(rows, header, tablefmt='grid')
+-------------+-------+--------+
| Major       |   GPA | Name   |
+=============+=======+========+
| Biology     |   2.4 | Edward |
+-------------+-------+--------+
| Physics     |   2.9 | Emily  |
+-------------+-------+--------+
| Mathematics |   3.5 | Sarah  |
+-------------+-------+--------+
&gt;&gt;&gt; print tabulate.tabulate(rows, header, tablefmt='rst')
===========  =====  ======
Major          GPA  Name
===========  =====  ======
Biology        2.4  Edward
Physics        2.9  Emily
Mathematics    3.5  Sarah
===========  =====  ======
</code></pre>

python