0

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

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Would this solution work if I added a third list? I can't get 3 lists to work for some reason.
Then you need to add another formatting: for t1, t2, t3 in zip(team1, team2, team3): print('%-20s %-20s %s' % (t1, t2, t3))
2

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.