I wrote this code with a couple of nested for loops, following instructions from the book im learning from "automate the boring stuff with python" and my output is vertical and not horizontally alligned:
#write a function that takes a list of lists of strings, and displays them in a well organized table with each
#column right justified. Assume the lists in list have the same number of items. (redundant?)
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(data):
#This is just for finding the longest lengths in the thing.
arrayOfLengths = [0]
length = 0
for list in data:
for words in list:
counter = 0
if length < len(words):
length = len(words)
arrayOfLengths.insert(counter, length)
counter += 1
#now to actually print the list
for list in data:
for words in list:
positionInIndex = 0
print(words.rjust( arrayOfLengths[positionInIndex]))
positionInIndex += 1
print()
printTable(tableData)
Also if you have any tips on how to make the program more efficient please do share!
Thank you!
ora1000x