-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrinting Lists
More file actions
27 lines (22 loc) · 818 Bytes
/
Printing Lists
File metadata and controls
27 lines (22 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Sample list :
>>> table_data
[['a', 'b', 'c'], ['aaaaaaaaaa', 'b', 'c'], ['a', 'bbbbbbbbbb', 'c']]
## Best and easiest way to pring the nested lists without much efforts.
>>> for row in table_data:
... print ("{:<20} {:<20} {:<20}".format(*row))
...
...
a b c
aaaaaaaaaa b c
a bbbbbbbbbb c
## Counting the longest value in nested list and then adjusting the output width accordingly.
>>> col_width = max(len(word) for row in data for word in row) + 2
>>> col_width
12
>>> for row in data:
... print "".join(word.ljust(col_width) for word in row)
...
...
a b c
aaaaaaaaaa b c
a bbbbbbbbbb c