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
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.
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.
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.
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
=========== ===== ======
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With