Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tabulate doesn't produce the correct table from dictionary

I am using Tabulate version 0.7.7 for Python 3.6

This is my code so far for a simple test using a dictionary.

from tabulate import tabulate

d = {"Dave":"13", "Bob":"15"}

headers = ["Name", "Age"]
print(tabulate(d, headers = headers))

The result I want is

Name    Age
------  -----
Dave    13
Bob     15

But what I am getting is

Name    Age
------  -----
1       1
3       5

Can someone help me please?

One question - Can I fix this using tabulate or should I be using a different python package?

like image 283
vik1245 Avatar asked Oct 16 '25 13:10

vik1245


1 Answers

You can access items attribute of dict. Like this. It'll give you result you want.

>>> print(tabulate(d.items(), headers = headers))
Name      Age
------  -----
Bob        15
Dave       13
like image 194
Darth Kotik Avatar answered Oct 19 '25 15:10

Darth Kotik