Print a right-justified list of a list I'm working through Chapter 6 in &quot;Automate the Boring Stuff, 2nd Edition.&quot; Here is the chapter project at the end.
Write a function named printTable() that takes a list of lists of strings and displays it in a well-organized table with each column right-justified. Assume that all the inner lists will contain the same number of strings. For example, the value could look like this:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'goose']]

Your printTable() function would print the following:

   apples Alice  dogs
  oranges   Bob  cats
 cherries Carol moose
   banana David goose

The first time I tackled this project, here was what I came up with:
#! python3
# Displays a table with each column right-justified.

table_data = [[&quot;apples&quot;, &quot;oranges&quot;, &quot;cherries&quot;, &quot;bananas&quot;],
    [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Carol&quot;, &quot;David&quot;],
    [&quot;dogs&quot;, &quot;cats&quot;, &quot;moose&quot;, &quot;goose&quot;]]

def print_table(table_data):
    # Create a new list of 3 &quot;0&quot; values: one for each list in table_data
    col_widths = [0] * len(table_data)
    # Search for the longest string in each list of table_data
    # and put the numbers of characters in the new list
    for y in range(len(table_data)):
        for x in table_data[y]:
            if col_widths[y] &lt; len(x):
                col_widths[y] = len(x)

    # Rotate and print the list of lists
    for x in range(len(table_data[0])):
        for y in range(len(table_data)):
            print(table_data[y][x].rjust(col_widths[y]), end=&quot; &quot;)
        print()

print_table(table_data)

The second time, I found out about the MAX method after several searches on Google. Here is what I came up with using the MAX method.
#! python3
# Displays a table with each column right-justified

table_data = [[&quot;apples&quot;, &quot;oranges&quot;, &quot;cherries&quot;, &quot;bananas&quot;],
            [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Carol&quot;, &quot;David&quot;],
            [&quot;dogs&quot;, &quot;cats&quot;, &quot;moose&quot;, &quot;goose&quot;]]

def print_table(table_data):
    # Create a new list of 3 &quot;0&quot; values: one for each list in table_data
    col_widths = [0] * len(table_data)
    # Search for longest string in each list of table_data
    # and put the numbers of characters in new list
    for y in range(len(table_data)):
        x = max(table_data[y], key=len)
        if col_widths[y] &lt; len(x):
            col_widths[y] = len(x)
    
    # Rotate and print the lists of lists
    for x in range(len(table_data[0])):
        for y in range(len(table_data)):
            print(table_data[y][x].rjust(col_widths[y]), end=&quot; &quot;)
        print()

print_table(table_data)

Any feedback would be greatly appreciated. Thanks!
You can transpose the given list of lists using a single line:
zip(*table_data)

That's it. Now, to find the longest word in each column, it would be another 1-liner:
map(len, [max(_, key=len) for _ in table_data])


You are calculating len(table_data) a lot of times. Better to store it as a variable? Although, another comprehension could be:
for row in zip(*table_data):
    for index, cell in enumerate(row):
        print(cell.rjust(col_width[index]), end=&quot; &quot;)


You whole function now becomes:
from typing import List
def print_table(table_data: List):
    col_width = list(map(len, [max(_, key=len) for _ in table_data]))
    for row in zip(*table_data):
        for index, cell in enumerate(row):
            print(cell.rjust(col_width[index]), end=&quot; &quot;)
        print()