Skip to content Skip to sidebar Skip to footer

How To Format A Dict Of Lists As A Table

I have a dict containing lists of strings and was wanting to print everything out as a table in the terminal in a format something like: +------------------------------------------

Solution 1:

You could use PrettyTable.

  • I sorted the keys, and sorted the values. This produces a predictable output.
  • I chose PrettyTable because it is installable in Ubuntu via apt-get install python-prettytable.

.

#! /usr/bin/env pythonfrom prettytable import PrettyTable

d1 = {
  "key1":["val1_1", "val1_2"],
  "key2":["val2_1", "val2_2"],
  "key3":["val3_1", "val3_2"],
  "key4":["val4_1", "val4_2"],
}

table = PrettyTable()

for key,val insorted(d1.iteritems()):
  table.add_column(key, sorted(val))

print table

The result is:

$ ./t
+--------+--------+--------+--------+
|  key1  |  key2  |  key3  |  key4  |
+--------+--------+--------+--------+
| val1_1 | val2_1 | val3_1 | val4_1 |
| val1_2 | val2_2 | val3_2 | val4_2 |
+--------+--------+--------+--------+

PrettyTable also provides HTML formatting. Replace the print table with:

printtable.get_html_string(attributes={"size":"100%", "class":"MyTable"})

and you get:

<tableborder="1"class="MyTable"size="100%"><tr><th>key1</th><th>key2</th><th>key3</th><th>key4</th></tr><tr><td>val1_1</td><td>val2_1</td><td>val3_1</td><td>val4_1</td></tr><tr><td>val1_2</td><td>val2_2</td><td>val3_2</td><td>val4_2</td></tr></table>

Solution 2:

TextTable works and can be installed via

pip install texttable

http://foutaise.org/code/texttable/

https://pypi.python.org/pypi?name=texttable&:action=display

Keys in a map are not sorted, so you are not guaranteed a particular order.

TextTable only accepts row-major table ordering, so you have to get a little fancy with the iterators:

import texttable
import itertools

theDict = {"key1": ["val_1", "val_2"],
           "key2": ["val_1", "val_2"],
           "key3": ["val_1", "val_2"],
           "key4": ["val_1", "val_2"]}

theTable = texttable.Texttable()
theIter = itertools.chain(
    iter([theDict.keys()]),
    itertools.imap(lambda *x: list(x), *theDict.itervalues())
    )

theTable.add_rows(theIter)
print theTable.draw()

Which gives:

+-------+-------+-------+-------+
| key3  | key2  | key1  | key4  |
+=======+=======+=======+=======+
| val_1 | val_1 | val_1 | val_1 |
+-------+-------+-------+-------+
| val_2 | val_2 | val_2 | val_2 |
+-------+-------+-------+-------+

Solution 3:

The problem with key1, key2, key3, etc is that the dicts in a key are not ordered. Hence, I felt so free as to change your dict into a list of key-value tuples in order to get an ordering into the keys:

You could then do something like this:

d = [ ('Coco', ['verde', 'redondo'] ),
     ('Plátano', ['amarillo', 'doblado'] ),
     ('Fresa', ['rojo', 'redondo chiquito'] ) ]

defmakeTable (d):
    widths = [2 + max ( [len (k) ] + [len (v) for v in vs] ) for k, vs in d]
    formats = ['{:^%s}' % width for width in widths]
    values = zip (* ( [k] + vs for k, vs in d) )
    sep = '\n+' + '-' * (sum (widths) + len (d) - 1) + '+\n'
    rows = sep.join ('|{}|'.format ('|'.join (formats [i].format (k) for i, k inenumerate (vs) ) ) for vs in values)
    return sep + rows + sep

print (makeTable (d) )

In order to convert a dict into a list of tuples, just use [ (k, v) for k, v in d.items () ] .

Putting these two things together would result in:

d = {'Coco': ['verde', 'redondo'],
     'Plátano': ['amarillo', 'doblado'],
     'Fresa': ['rojo', 'redondo chiquito'] }

print (makeTable ( [ (k, v) for k, v in d.items () ] ) )

Solution 4:

If you only need it to view the content of your dictionary in a well-formatted way, you can give a try to the Python dict formatter and viewer which seems to be exactly for that - it can display the dict (or, a list of dicts) as a table or a tree.

Post a Comment for "How To Format A Dict Of Lists As A Table"