Python how to print pretty table from dictionary or list
Python offers several different ways of beautifying the output of dictionary or list. You can add different formatting in order to make output better and much more easier to read. Lets check several examples:
- Python pretty print from dictionary
- Python pretty print from list of list
- Python pretty print from list with tabulate
- Python pretty print from list/dict with pandas
Python pretty print from dictionary
Very frequently the information is extracted in a dictionary. In order to make the output easier for reading you need to do several small tricks. One way is by using format functions of strings. The example below demonstrate it:
d = {1: ["Python", 33.2, 'UP'],
2: ["Java", 23.54, 'DOWN'],
3: ["Ruby", 17.22, 'UP'],
10: ["Lua", 10.55, 'DOWN'],
5: ["Groovy", 9.22, 'DOWN'],
6: ["C", 1.55, 'UP']
}
print ("{:<8} {:<15} {:<10} {:<10}".format('Pos','Lang','Percent','Change'))
for k, v in d.items():
lang, perc, change = v
print ("{:<8} {:<15} {:<10} {:<10}".format(k, lang, perc, change))
result:
Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
If you print without formatting you will get ugly and unreadable output like:
Pos, Lang, Percent, Change
1, Python, 33.2, UP
2, Java, 23.54, DOWN
3, Ruby, 17.22, UP
5, Groovy, 9.22, DOWN
6, C, 1.55, UP
10, Lua, 10.55, DOWN
Python pretty print from list of list
If you want to print information as a score table team against team like:
Liquid Virtus.pro
Liquid 1 2
Virtus.pro x 1
You can do it by using method format again:
dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * (len(dota_teams) + 1)
print(format_row.format("", *dota_teams))
for team, row in zip(dota_teams, data):
print(format_row.format(team, *row))
result:
Liquid Virtus.pro PSG.LGD Team Secret
Liquid 1 2 1 x
Virtus.pro x 1 1 x
PSG.LGD 1 x 0 1
Team Secret 2 0 2 1
Python pretty print from list with tabulate
You can use python packages like: tabulate. The advantage of using packages lie tabulate for pretty print of lists and dictionaries is: there's no need to do custom settings and format every time you add new item to the list. In other words this is not hardcoded solution:
from tabulate import tabulate
data = [[1, 'Liquid', 24, 12],
[2, 'Virtus.pro', 19, 14],
[3, 'PSG.LGD', 15, 19],
[4,'Team Secret', 10, 20]]
print (tabulate(data, headers=["Pos", "Team", "Win", "Lose"]))
This would result in:
Pos Team Win Lose
----- ----------- ----- ------
1 Liquid 24 12
2 Virtus.pro 19 14
3 PSG.LGD 15 19
4 Team Secret 10 20
There are other solutions for pretty print of list or dict with Python like: PrettyTable, texttable, beautifultable. You can check if they fit your needs better.
Python pretty print from list/dict with pandas
Pandas is another good solution if you want to print out your data in good looking table which allows many customizations and support many different formats like: csv, dict, json etc.
import pandas
data = [[1, 'Liquid', 24, 12],
[2, 'Virtus.pro', 19, 14],
[3, 'PSG.LGD', 15, 19],
[4,'Team Secret', 10, 20]]
headers=["Pos", "Team", "Win", "Lose"]
print(pandas.DataFrame(data, headers, headers))
print(pandas.DataFrame(data, headers))
This first code will result in:
pandas.DataFrame(data, headers, headers)
Pos Team Win Lose
Pos 1 Liquid 24 12
Team 2 Virtus.pro 19 14
Win 3 PSG.LGD 15 19
Lose 4 Team Secret 10 20
This second one will result in:
pandas.DataFrame(data, headers)
0 1 2 3
Pos 1 Liquid 24 12
Team 2 Virtus.pro 19 14
Win 3 PSG.LGD 15 19
Lose 4 Team Secret 10 20