Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python convert array of objects with k, v pairs into table

I’m looking for way to convert the beneath list of dicts into a table.

From:

arr = [{'G': ["Apple", "Banana"]}, {'M': ["Orange", "Kiwi"]}, {'P': ["Orange"]}, {'MP': ["All"]}]

To:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

User Fruit
G Apple, Banana
M Orange, Kiwi
P Orange
MP All

I’d also like to have the totals of each fruit detailed.

Beneath is my current solution, I’m making use of tabulate

table = tabulate(arr, tablefmt='html')

Any help would be greatly appreciated.

>Solution :

table = tabulate([ [ list(arr[i].keys())[0] , ','.join(arr[i].get(list(arr[i].keys())[0])) ] for i in range(len(arr)) ], tablefmt='html', headers=["User","Fruit"])

try this

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading