The practice problem asks that I arrange the nested lists in tabledata into right justified columns with one nested list each. I've figured out how to right justify every word in each column to the correct length of the column itself, but I can't figure out how to join the columns into a list instead of just printing them out as one column.
I've tried concatenating the strings in the for-loop by iterating it ahead of time (print(list[i]+list[i+1]+list[i+2]...etc), but I keep getting index out of range errors when it loops through again and increments i.
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def longlen(strings):
maximum = 0
for s in strings:
if len(s) > maximum:
maximum = len(s)
return(maximum)
def printTable(table):
column_width = [0] * len(table)
for i in range(len(table)):
column_width[i] =longlen(table[i])
for x in range(len(table[i])):
print(table[i][x].rjust(column_width[i]))
printTable(tableData)
Right now I am getting the correct right justification for each word, but I cant quite figure out how to output this in a 4 tall by 3 wide right justified table.
Right now it looks like this:
apples
oranges
cherries
banana
Alice
Bob
Carol
David
dogs
cats
moose
goose
I need this:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose