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

How to print list vertical and not horizaontal with tabulate package?

I try to print the results of a list vertical. But they are at the moment display horizontal.

So I try it as code:

def extract_data_excel_combined(self):
        dict_fruit = {"Watermeloen": 3588.20, "Appel": 5018.75, "Sinaasappel": 3488.16}
        fruit_list = list(dict_fruit.values())
        new_fruit_list = []
        new_fruit_list.append((fruit_list))    
        columns = ["totaal kosten fruit"]

       
        return mark_safe(
            tabulate(new_fruit_list, headers=columns, tablefmt="html", stralign="center")
        )

this results in this format:

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

    totaal kosten fruit
3588.2  5018.75 3488.16

But I want to have them:

totaal kosten fruit
3588.2  
5018.75 
3488.16

Question: how to display the list items vertical?

>Solution :

You have your fruit list like as:
[[3588.2, 5018.75, 3488.16]]

Basically a list in a list.
It should be like that:
[[3588.2], [5018.75], [3488.16]]

So three lists in a list.

dict_fruit = {"Watermeloen": 3588.20, "Appel": 5018.75, "Sinaasappel": 3488.16}
fruit_list = [[i]for i in dict_fruit.values()]
columns = ["totaal kosten fruit"]
tabulate(fruit_list, headers=columns, tablefmt="html", stralign="center")
Output:
totaal kosten fruit
3588.2
5018.75
3488.16

And another thing to mention, I don’t like your columns = ["totaal kosten fruit"], it should be divided, the resulting code would be:

dict_fruit = {"Watermeloen": 3588.20, "Appel": 5018.75, "Sinaasappel": 3488.16}
fruit_list = [[i,'','']for i in dict_fruit.values()]
columns = ["totaal","kosten","fruit"]
tabulate(fruit_list, headers=columns, tablefmt="html", stralign="center")
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