Skip to main content
Add 200_success' comment. Silly me, ):
Source Link
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155

The way that you find rows and columns is good. You can't iterate through the lists so this approach is good. However using them for your comprehensions is bad.

The name a_list is not descriptive, and quite poor. table whilst generic is much better.

Your comprehensions and loops are actually rather poor. Your comprehensions are used as if you are initialize an array in say C. But this is Python, not C.

You should also use the builtins. The way you find the longest in a list can be changed to use max instead. And if you use a comprehension to change all the strings to their length then you could make column_widths a comprehension.

column_widths = [max(len(icell) for icell in rowcolumn) for rowcolumn in table]

Your print row is also quite strange. Instead of print_row[a] = print_row[a] + you should use print_row[a] += but this can be done in a list comprehension too. As you're joining the items by nothing you can 'wrap' the comprehension with ''.join. And the comprehension would just be the indexing the table and justifying you're already doing. This can result in:

print_row = ["" for i in range(rows)]
for row in range(rows):
    print_row[row] = ''.join(
        table[col][row].rjust(column_widths[col])
        for col in range(columns)
    )

for item in print_row:
    print(item)

From this you should notice that there is not much point in adding to a list, if you're just going to print it later anyway. And so you can merge these two loops together and remove the print_row comprehension.

Also I'd change the rjust call to not add 4 spaces, as that indents the table, which looks kinda ugly. And you can add a space between each column by using ' '.join instead. This can result in the function:

def print_table(table):
    rows, columns = len(table[0]), len(table)
    
    column_widths = [max(len(icell) for icell in rowcolumn) for rowcolumn in table]
    for row in range(rows):
        print(' '.join(table[col][row].rjust(column_widths[col]) for col in range(columns)))

The way that you find rows and columns is good. You can't iterate through the lists so this approach is good. However using them for your comprehensions is bad.

The name a_list is not descriptive, and quite poor. table whilst generic is much better.

Your comprehensions and loops are actually rather poor. Your comprehensions are used as if you are initialize an array in say C. But this is Python, not C.

You should also use the builtins. The way you find the longest in a list can be changed to use max instead. And if you use a comprehension to change all the strings to their length then you could make column_widths a comprehension.

column_widths = [max(len(i) for i in row) for row in table]

Your print row is also quite strange. Instead of print_row[a] = print_row[a] + you should use print_row[a] += but this can be done in a list comprehension too. As you're joining the items by nothing you can 'wrap' the comprehension with ''.join. And the comprehension would just be the indexing the table and justifying you're already doing. This can result in:

print_row = ["" for i in range(rows)]
for row in range(rows):
    print_row[row] = ''.join(
        table[col][row].rjust(column_widths[col])
        for col in range(columns)
    )

for item in print_row:
    print(item)

From this you should notice that there is not much point in adding to a list, if you're just going to print it later anyway. And so you can merge these two loops together and remove the print_row comprehension.

Also I'd change the rjust call to not add 4 spaces, as that indents the table, which looks kinda ugly. And you can add a space between each column by using ' '.join instead. This can result in the function:

def print_table(table):
    rows, columns = len(table[0]), len(table)
    
    column_widths = [max(len(i) for i in row) for row in table]
    for row in range(rows):
        print(' '.join(table[col][row].rjust(column_widths[col]) for col in range(columns)))

The way that you find rows and columns is good. You can't iterate through the lists so this approach is good. However using them for your comprehensions is bad.

The name a_list is not descriptive, and quite poor. table whilst generic is much better.

Your comprehensions and loops are actually rather poor. Your comprehensions are used as if you are initialize an array in say C. But this is Python, not C.

You should also use the builtins. The way you find the longest in a list can be changed to use max instead. And if you use a comprehension to change all the strings to their length then you could make column_widths a comprehension.

column_widths = [max(len(cell) for cell in column) for column in table]

Your print row is also quite strange. Instead of print_row[a] = print_row[a] + you should use print_row[a] += but this can be done in a list comprehension too. As you're joining the items by nothing you can 'wrap' the comprehension with ''.join. And the comprehension would just be the indexing the table and justifying you're already doing. This can result in:

print_row = ["" for i in range(rows)]
for row in range(rows):
    print_row[row] = ''.join(
        table[col][row].rjust(column_widths[col])
        for col in range(columns)
    )

for item in print_row:
    print(item)

From this you should notice that there is not much point in adding to a list, if you're just going to print it later anyway. And so you can merge these two loops together and remove the print_row comprehension.

Also I'd change the rjust call to not add 4 spaces, as that indents the table, which looks kinda ugly. And you can add a space between each column by using ' '.join instead. This can result in the function:

def print_table(table):
    rows, columns = len(table[0]), len(table)
    
    column_widths = [max(len(cell) for cell in column) for column in table]
    for row in range(rows):
        print(' '.join(table[col][row].rjust(column_widths[col]) for col in range(columns)))
Source Link
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155

The way that you find rows and columns is good. You can't iterate through the lists so this approach is good. However using them for your comprehensions is bad.

The name a_list is not descriptive, and quite poor. table whilst generic is much better.

Your comprehensions and loops are actually rather poor. Your comprehensions are used as if you are initialize an array in say C. But this is Python, not C.

You should also use the builtins. The way you find the longest in a list can be changed to use max instead. And if you use a comprehension to change all the strings to their length then you could make column_widths a comprehension.

column_widths = [max(len(i) for i in row) for row in table]

Your print row is also quite strange. Instead of print_row[a] = print_row[a] + you should use print_row[a] += but this can be done in a list comprehension too. As you're joining the items by nothing you can 'wrap' the comprehension with ''.join. And the comprehension would just be the indexing the table and justifying you're already doing. This can result in:

print_row = ["" for i in range(rows)]
for row in range(rows):
    print_row[row] = ''.join(
        table[col][row].rjust(column_widths[col])
        for col in range(columns)
    )

for item in print_row:
    print(item)

From this you should notice that there is not much point in adding to a list, if you're just going to print it later anyway. And so you can merge these two loops together and remove the print_row comprehension.

Also I'd change the rjust call to not add 4 spaces, as that indents the table, which looks kinda ugly. And you can add a space between each column by using ' '.join instead. This can result in the function:

def print_table(table):
    rows, columns = len(table[0]), len(table)
    
    column_widths = [max(len(i) for i in row) for row in table]
    for row in range(rows):
        print(' '.join(table[col][row].rjust(column_widths[col]) for col in range(columns)))