Jack
Jack

Reputation: 45

How can I change align columns vertically in python?

I have two Lists:

team1 = ['Vàlentine', 'Consus', 'Never Casual ', 'NucIear', 'Daltwon']
team2 = ['The Aviator', 'Iley', 'Nisquick', 'Dragoon', 'WAACK']

And I want to display the contents of these lists as follows:

team1(bold)     team2(bold)
Valentine       The Aviator
Consus          Iley
Never Casual    Nisquick
Nuclear         Dragoon
Daltwon         WAACK

And I'd want the code to be able to work with multiple lists.

I've currently tried this piece of code, which almost works, but I'm not sure how to configure it so that the columns after the first column are aligned.

L = [team1,team2]
max_length = max(map(len, L))  # finding max length
output = zip(*map(lambda x: x + [' '] * (max_length - len(x)), L))  # filling every sublist with ' ' to max_length and then zipping it
for i in output: print(*i, sep= '                  ')

output:

Valentine                   The Aviator
Consus                 Iley
Never Casual                  Nisquick
NucIear                  Dragoon
Daltwon                   WAACK

Upvotes: 0

Views: 2133

Answers (4)

Albin Paul
Albin Paul

Reputation: 3419

If you use python3 then you can use fstring for formatting also :)

team1 = ['Vàlentine', 'Consus', 'Never Casual ', 'NucIear', 'Daltwon']
team2 = ['The Aviator', 'Iley', 'Nisquick', 'Dragoon', 'WAACK']
maxlen = max(map(len,team1)) + 1 
for a,b in zip(team1,team2):
    print(f"{a: <{maxlen}} {b}")

Gives

Vàlentine      The Aviator
Consus         Iley
Never Casual   Nisquick
NucIear        Dragoon
Daltwon        WAACK

Upvotes: 2

SM Abu Taher Asif
SM Abu Taher Asif

Reputation: 2331

I have made a bit modification to your code and count number of space needed for each line the print the line:

team1 = ['Vàlentine', 'Consus', 'Never Casual ', 'NucIear', 'Daltwon']
team2 = ['The Aviator', 'Iley', 'Nisquick', 'Dragoon', 'WAACK']
max_length = max(map(len, team1))  # finding max length
for (t1,t2) in zip(team1,team2):
    numOfSpace = max_length - len(t1) + 5 # 5 is the minimum space, you can change it
    space = ""
    for i in range(numOfSpace):
        space = space+ " "
    print(t1, space, t2)

output:

Vàlentine           The Aviator
Consus              Iley
Never Casual        Nisquick
NucIear             Dragoon
Daltwon             WAACK

Upvotes: 0

venkata krishnan
venkata krishnan

Reputation: 2046

you can use the ljust function in python. This function lets you to add a definite number of spaces to a string. So knowing the maximum (a rough estimate) of the length of your first string, you can format it like this.

string.ljust(max_len)

example

"hi".ljust(10) will give you "hi        "

"hello".ljust(10) will give you "hello     "

you can also use rjust if you want the content to be aligned on the right side.

Upvotes: 0

Chris
Chris

Reputation: 29742

Use string formatting:

team1 = ['Vàlentine', 'Consus', 'Never Casual ', 'NucIear', 'Daltwon']
team2 = ['The Aviator', 'Iley', 'Nisquick', 'Dragoon', 'WAACK']

for t1, t2 in zip(team1, team2):
    print('%-20s %s' % (t1, t2))

Output:

Vàlentine            The Aviator
Consus               Iley
Never Casual         Nisquick
NucIear              Dragoon
Daltwon              WAACK

Upvotes: 3

Related Questions