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

Time:12-14

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:

    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?

CodePudding user response:

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")
  • Related