2

I have the following 2D list:

table = [['Position', 'Club', 'MP', 'GD', 'Points'], 
         ['1', 'Man City', '38', '51', '86'], 
         ['2', 'Man Utd', '38', '29', '74'], 
         ['3', 'Liverpool', '38', '26', '69'], 
         ['4', 'Chelsea', '38', '22', '67'], 
         ['5', 'Leicester', '38', '18', '66']]

I am wanting to print it so that the format is as following:

Position       Club           MP             GD             Points
1              Man City       38             51             86
2              Man Utd        38             29             74
3              Liverpool      38             26             69              
4              Chelsea        38             22             67      
5              Leicester      38             18             66         

My issue is with the even spacing. My attempt at solving this was using:

for i in range(len(table)):
     print(*table[i], sep=" "*(15-len(table[i])))  

However, I realised that problem is that 'i' refers to the number of items within each row, rather than the length of each individual item, which is what it would take to make even spacing - I think.

How can I get my desired format? And is my approach okay or is there a much better way of approaching this?

I have looked at this - 2D list output formatting - which helped with aspects but not with the even spacing problem I don't think

Any help would be much appreciated, thank you!

BenjieSS
  • 35
  • 5
  • 1
    You should use string formatting, as in the answer below, but note that `i` doesn't refer to the number of items within each row, `i` is an `int`, in this case, you are getting each of the indices into your list. But *don't iterate over a `range` object here to begin with*, there's no point, use `for row in table: ....` and then work with `row` – juanpa.arrivillaga Aug 19 '21 at 20:51

2 Answers2

2

You can use str.format for formatting the table (documentation):

table = [
    ["Position", "Club", "MP", "GD", "Points"],
    ["1", "Man City", "38", "51", "86"],
    ["2", "Man Utd", "38", "29", "74"],
    ["3", "Liverpool", "38", "26", "69"],
    ["4", "Chelsea", "38", "22", "67"],
    ["5", "Leicester", "38", "18", "66"],
]

format_string = "{:<15}" * 5

for row in table:
    print(format_string.format(*row))

Prints:

Position       Club           MP             GD             Points         
1              Man City       38             51             86             
2              Man Utd        38             29             74             
3              Liverpool      38             26             69             
4              Chelsea        38             22             67             
5              Leicester      38             18             66             
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

You should use the string formatting approach, but the problem with your current solution is that you need to consider the length of each item in the row. So you want something like:

for row in table:
    for item in row:
        print(item, end=' '*(15 - len(item)))
    print()

Note, you almost never want to use for index in range(len(some_list)):, instead, iterate directly over the list. Even in the cases where you do need the index, you almost certainly would end up using enumerate instead of range. Here's the equivalent using ranges... but again, you wouldn't do it this way, it isn't Pythonic:

for i in range(len(table)):
    for j in range(len(table[i])):
        print(table[i][j], end=' '*(15 - len(table[i][j])))
    print()
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172