Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting lists
#1
How can i format lists in columns so that gap between them are even any they are nicely in line?

Thanks.
Reply
#2
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:
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:
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
rturus likes this post
Reply
#3
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
Reply
#4
Use the table master ๐Ÿ“œ

f-string just try some values to get a fit.
lst = [
    ["Name", "Email", "ID"],
    ["Richard", "[email protected]", "55"],
    ["Tasha", "[email protected]", "99"]
]

for inner in lst:
    print(' | '.join((f"{word:22}" for word in inner)))

Output:
Name | Email | ID Richard | [email protected] | 55 Tasha | [email protected] | 99
Could use a max length as shown over to.
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 | [email protected] | 55 Tasha | [email protected] | 99

Tabulate as shown work fine for this.
from tabulate import tabulate

lst = [
    ["Name", "Email", "ID"],
    ["Richard", "[email protected]", "55"],
    ["Tasha", "[email protected]", "99"]
]

print(tabulate(lst))
print(tabulate(lst, tablefmt="fancy_grid"))
print(tabulate(lst, tablefmt="github"))
Output:
------- --------------------- -- Name Email ID Richard [email protected] 55 Tasha [email protected] 99 ------- --------------------- -- โ•’โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•• โ”‚ Name โ”‚ Email โ”‚ ID โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”ค โ”‚ Richard โ”‚ [email protected] โ”‚ 55 โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”ค โ”‚ Tasha โ”‚ [email protected] โ”‚ 99 โ”‚ โ•˜โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•› |---------|-----------------------|----| | Name | Email | ID | | Richard | [email protected] | 55 | | Tasha | [email protected] | 99 |
Reply
#5
(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.


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']]



 #a=0, a=1,a=2, a=3, a=4,a=5
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
Reply
#6
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.

list_a=[[1,2,3,4,5],
        ['d','h','f','s','g']]



 #a=0, a=1,a=2, a=3, a=4,a=5
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020