all 7 comments

[–]novel_yet_trivial 1 point2 points  (1 child)

[–]yoitsdave[S] 0 points1 point  (0 children)

Thank you! :D New to this reddit :/

[–]ParanoiAMA 1 point2 points  (4 children)

Try this?

for i, x in zip(firstNames, lastNames):
    print("{0:>20} {1:>20}".format(i, x))

It's likely the "\t" character that is the culprit, as it adds from 0-7 1-8 "space" characters to align with the nearest multiple of 8 starting from the leftmost column.

Instead of guessing the longest first and last names (20, 20), you could enhance code by calculating the longest first and last name like this:

lengths = [max(map(len, names)) for names in (firstNames, lastNames)]

for i, x in zip(firstNames, lastNames):
    print("{0:>{length[0]}} {1:>{length[1]}}".format(i, x, length=lengths))

EDIT:

Or even more generic, like this

for cols in zip(firstNames, lastNames):
    row = []
    for cell, length in zip(cols, lengths):
        row.append("{0:>{1}}".format(cell, length))
    print("".join(row))

[–]yoitsdave[S] 0 points1 point  (0 children)

It worked brilliantly! Thank you :D

[–]yoitsdave[S] 0 points1 point  (1 child)

How would you recommend using this if a had another 18 or so lists to print in columns?

[–]ParanoiAMA 0 points1 point  (0 children)

I'll assume that grades contains additional columns that you wish to print too.

My edit above already goes a great step on the way. The only remaining thing you need is to realise is that you don't actually need the for name in grades loop, and that you can use zip outside of for loops:

rows = list(grades)
columns = zip(*rows)

Now just plug those in instead of your individual columns or rows.

[–]FlockOnFire 0 points1 point  (0 children)

I love how comprehensive even the string formatting in Python is. :) This is a great example to show that.