python-tabulate icon indicating copy to clipboard operation
python-tabulate copied to clipboard

Documentation missing for using a dictionary as the header for a list of dictionaries

Open Quietus opened this issue 3 years ago • 2 comments

When using tabulate with a list of dictionaries, it gives an error if the header is not a keyword or a dictionary. There is documentation for the keyword options but nothing for the dictionary as header option.

A short explanation and an example would be very helpful e.g.:

When tabulating a list of dictionaries, a dictionary can be used to replace the labels for keys in your list of dictionaries - for example:

samplelistdict=[ { "key1" : "value1", "key2": "value2" },{ "key1" : "value11", "key2": "value12" } ]
sampleheaderdict={"key1": "Bettername", "key2": "Better2"}
print(tabulate(samplelistdict, sampleheaderdict))

Produces:

Bettername    Better2
------------  ---------
value1        value2
value11       value12

Quietus avatar Oct 19 '22 11:10 Quietus

When this feature is documented, it may be useful to suggest use of the ordered dictionaries. Using an ordinary dictionary for headers or data confuses some users when they don't see columns in the same order they expected them to be seen.

astanin avatar Oct 19 '22 12:10 astanin

Actually, python preserves the insertion order of dictionaries since 3.7 (officially) so they are safe to use now.

This means the example in README about using keys in headers should be changed since the column labels are swapped there which should no longer happen.

I did this in the same PR which fixes the current issue.

ilya112358 avatar Dec 29 '22 13:12 ilya112358