Print a list of tuples as table
Using Format Specification Mini-Language
print "HEADER1 HEADER2 HEADER3"
for ele1,ele2,ele3 in l:
print "{:<14}{:<11}{}".format(ele1,ele2,ele3)
HEADER1 HEADER2 HEADER3
element1 element2 element3
elementelel4 element5 elementelement6
el7 el8 elel9
You can use ljust
for left-aligning the element within a fixed width
column_width=20
for row in list:
for el in row:
print el.ljust(column_width),
print
output
element1 element2 element3
elementelel4 element5 elementelement6
el7 el8 elel9
Assume that, all tuples in the list is of the same length, you can use this:
l = [('element1', 'element2', 'element3'), ('elementelel4', 'element5', 'elementelement6'), ('el7', 'el8', 'elel9')]
width = max(len(e) for t in l for e in t[:-1]) + 1
format=('%%-%ds' % width) * len(l[0])
print '\n'.join(format % tuple(t) for t in l)
Output:
element1 element2 element3
elementelel4 element5 elementelement6
el7 el8 elel9