Posts: 37
Threads: 18
Joined: Jan 2017
How can i format lists in columns so that gap between them are even any they are nicely in line?
Thanks.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Use a formatting package. As an example, you could look at tabulate, but there are several others.
Otherwise, you need to calculate the size of your fields yourself and enforce that size with formatting commands.
Example with tabulate:
1 2 3 4 5 6 7 |
from tabulate import tabulate
table = [[ "A" , "B" , "C" ],
[ "D" , "Epsilon" , "Foxtrot city" ],
[ "South Mangrove" , "T" , "U" ]]
print (tabulate(table))
|
Output: -------------- ------- ------------
A B C
D Epsilon Foxtrot city
South Mangrove T U
-------------- ------- ------------
Manual example:
1 2 3 4 5 6 |
table = [[ "A" , "B" , "C" ],
[ "D" , "Epsilon" , "Foxtrot city" ],
[ "South Mangrove" , "T" , "U" ]]
for row in table:
print ( "{:15} {:15} {:15}" . format ( * row))
|
Output: A B C
D Epsilon Foxtrot city
South Mangrove T U
Posts: 1,034
Threads: 16
Joined: Dec 2016
1 2 3 4 5 6 7 8 9 10 11 |
table = [[ "A" , "B" , "C" ],
[ "D" , "Epsilon" , "Foxtrot city" ],
[ "South Mangrove" , "T" , "U" ]]
list_len = [ len (element) for row in table for element in row]
column_width = max (list_len)
for row in table:
row = "".join(element.ljust(column_width + 2 ) for element in row)
print (row)
|
Output: A B C
D Epsilon Foxtrot city
South Mangrove T U
Posts: 7,324
Threads: 123
Joined: Sep 2016
Use the table master 📜
f-string just try some values to get a fit.
1 2 3 4 5 6 7 8 |
lst = [
[ "Name" , "Email" , "ID" ],
[ "Richard" , "richard@fakeemail.com" , "55" ],
[ "Tasha" , "tash@fakeemail.com" , "99" ]
]
for inner in lst:
print ( ' | ' .join(( f "{word:22}" for word in inner)))
|
Output: Name | Email | ID
Richard | richard@fakeemail.com | 55
Tasha | tash@fakeemail.com | 99
Could use a max length as shown over to.
1 2 3 |
max_len = max ( len (i) for j in lst for i in j)
for inner in lst:
print ( ' | ' .join(( f "{word:{max_len}}" for word in inner)))
|
Output: Name | Email | ID
Richard | richard@fakeemail.com | 55
Tasha | tash@fakeemail.com | 99
Tabulate as shown work fine for this.
1 2 3 4 5 6 7 8 9 10 11 |
from tabulate import tabulate
lst = [
[ "Name" , "Email" , "ID" ],
[ "Richard" , "richard@fakeemail.com" , "55" ],
[ "Tasha" , "tash@fakeemail.com" , "99" ]
]
print (tabulate(lst))
print (tabulate(lst, tablefmt = "fancy_grid" ))
print (tabulate(lst, tablefmt = "github" ))
|
Output: ------- --------------------- --
Name Email ID
Richard richard@fakeemail.com 55
Tasha tash@fakeemail.com 99
------- --------------------- --
╒═════════╤═══════════════════════╤════╕
│ Name │ Email │ ID │
├─────────┼───────────────────────┼────┤
│ Richard │ richard@fakeemail.com │ 55 │
├─────────┼───────────────────────┼────┤
│ Tasha │ tash@fakeemail.com │ 99 │
╘═════════╧═══════════════════════╧════╛
|---------|-----------------------|----|
| Name | Email | ID |
| Richard | richard@fakeemail.com | 55 |
| Tasha | tash@fakeemail.com | 99 |
Posts: 37
Threads: 18
Joined: Jan 2017
(Nov-24-2020, 11:38 PM)bowlofred Wrote: Use a formatting package. As an example, you could look at tabulate, but there are several others.
Otherwise, you need to calculate the size of your fields yourself and enforce that size with formatting commands.
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 28 29 30 31 32 33 34 35 36 |
table = [[ "A" , "B" , "C" ],
[ "D" , "Epsilon" , "Foxtrot city" ],
[ "South Mangrove" , "T" , "U" ]]
Thanks for that. I just get some thing a bit different
list_a = [[ 1 , 2 , 3 , 4 , 5 ],
[ 'd' , 'h' , 'f' , 's' , 'g' ]]
for row in list_a:
print ( "{:15} {:15} {:15} {:15}{:15} " . format ( * row))]])
[output]
1 2 3 4 5
d h f s g
any idea why 1 and d are not in line and the rest?
Thanks.
[output] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
A B C
D Epsilon Foxtrot city
South Mangrove T U
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - [ / output]
Manual example:
[python]table = [[ "A" , "B" , "C" ],
[ "D" , "Epsilon" , "Foxtrot city" ],
[ "South Mangrove" , "T" , "U" ]]
for row in table:
print ( "{:15} {:15} {:15}" . format ( * row))
|
Output: A B C
D Epsilon Foxtrot city
South Mangrove T U
Posts: 1,583
Threads: 3
Joined: Mar 2020
It's because you're passing in two different data types, int and str , and the default for format is to left-justify strings, but right-justify numbers. So they end up on the opposite side.
There's no right way to do this. Shrinking the size as small as possible helps. Changing the ints to strs works okay, but you lose the numeric alignment. For small numbers this is okay, but if you have a mix of sizes, it's very confusing.
1 2 3 4 5 6 7 8 |
list_a = [[ 1 , 2 , 3 , 4 , 5 ],
[ 'd' , 'h' , 'f' , 's' , 'g' ]]
for row in list_a:
print ( "{:15} {:15} {:15} {:15}{:15} " . format ( * ( str (x) for x in row)))
|
Output: 1 2 3 4 5
d h f s g
|