Python Forum
Formatting lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Formatting lists (/thread-31143.html)



Formatting lists - rturus - Nov-24-2020

How can i format lists in columns so that gap between them are even any they are nicely in line?

Thanks.


RE: Formatting lists - bowlofred - Nov-24-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:
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



RE: Formatting lists - Axel_Erfurt - Nov-25-2020

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



RE: Formatting lists - snippsat - Nov-25-2020

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 |



RE: Formatting lists - rturus - Nov-26-2020

(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



RE: Formatting lists - bowlofred - Nov-26-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.

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