Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print a list of dictionaries in table form

Tags:

python

assume I have this list as global list

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

and a want a function print() to print it as

 name     major        GPA 
 =============================== 
 edward   Biology      2.4 
 Emily    physics      2.9 
 sarah    mathematics  3.5
like image 567
Huda Avatar asked Oct 15 '16 08:10

Huda


People also ask

How do you print a dictionary list in Python?

Python's dict. keys() method can be used to retrieve the dictionary keys, which can then be printed using the print() function. A view object that shows a list of every key in the dictionary is the result of the dict. keys() method.

Can you make an array of dictionaries?

Creating an Array of DictionariesAn array of dictionaries is no different. The array literal is a bit more complex, but that is the only difference. In this example, we create an array of dictionaries using an array literal. We don't need to specify the type of the array thanks to type inference.

Can you sort a list of dictionaries?

To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function.


3 Answers

you can use module tabulate. The following code is compatible with python 2, for python 3 see in the comments.

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

you can use tablefmt parameter for different table format

>>> print tabulate.tabulate(rows, header, tablefmt='grid')
+-------------+-------+--------+
| Major       |   GPA | Name   |
+=============+=======+========+
| Biology     |   2.4 | Edward |
+-------------+-------+--------+
| Physics     |   2.9 | Emily  |
+-------------+-------+--------+
| Mathematics |   3.5 | Sarah  |
+-------------+-------+--------+
>>> print tabulate.tabulate(rows, header, tablefmt='rst')
===========  =====  ======
Major          GPA  Name
===========  =====  ======
Biology        2.4  Edward
Physics        2.9  Emily
Mathematics    3.5  Sarah
===========  =====  ======
like image 102
Hackaholic Avatar answered Nov 06 '22 05:11

Hackaholic


for dicts in dataset:
    print(dicts.get('Name')),
    print(dicts.get('Major')),
    print(dicts.get('GPA')),

Example

>>> dataset =[{'Major': 'Biology', 'GPA': '2.4', 'Name': 'Edward'}, {'Major': 'Physics', 'GPA': '2.9', 'Name': 'Emily'}, {'Major': 'Mathematics', 'GPA': '3.5', 'Name': 'Sarah'}]
>>> 
>>> for dicts in dataset:
...         print(dicts.get('Name')),
...         print(dicts.get('Major')),
...         print(dicts.get('GPA')),
... 
Edward Biology 2.4 Emily Physics 2.9 Sarah Mathematics 3.5
like image 41
Lokesh Sanapalli Avatar answered Nov 06 '22 07:11

Lokesh Sanapalli


How about this:

from __future__ import print_function

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

[print("%s %s: %s\n"%(item['Name'],item['Major'],item['GPA'])) for item in dataset]

result:

Edward Biology: 2.4

Emily Physics: 2.9

Sarah Mathematics: 3.5
like image 21
Kennet Celeste Avatar answered Nov 06 '22 06:11

Kennet Celeste