Automate the Boring Stuff Chapter 6 Table Printer Almost Done












5















In this section, they want us to create this table:



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


It must be justified to the right, and the input is tableData. Here's my code:



tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
listlens=
tour=0
lists={}
for m in tableData:
total=0
tour+=1
for n in m:
total+=len(n)
lists["list:",tour]=total
print("list",tour,total)

itemcount=list(lists.values())
sortedlen=(sorted(itemcount,reverse=True))
longest=sortedlen[0]

#print (lists['list:', 1])
#print (longest)


for m in range(len(tableData[0])):
for n in range(len(tableData)):
print (tableData[n][m],end=" ")
n+=1
print ("".rjust(lists['list:', 1],"-"))
m+=1


I'm almost done except for one thing, I can't make it right-justified. This output is the closest I came so far.



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


If I put rjust inside the inner for-loop the output is much different:



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









share|improve this question


















  • 1





    Have you tried joining first?

    – Ignacio Vazquez-Abrams
    Dec 28 '15 at 5:34











  • Could you explain a little bit more? Do you mean at the end of the print function or in the for loop?

    – Stanley Wilkins
    Dec 28 '15 at 6:50











  • I tried it now, but got argument error.

    – Stanley Wilkins
    Dec 28 '15 at 7:11
















5















In this section, they want us to create this table:



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


It must be justified to the right, and the input is tableData. Here's my code:



tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
listlens=
tour=0
lists={}
for m in tableData:
total=0
tour+=1
for n in m:
total+=len(n)
lists["list:",tour]=total
print("list",tour,total)

itemcount=list(lists.values())
sortedlen=(sorted(itemcount,reverse=True))
longest=sortedlen[0]

#print (lists['list:', 1])
#print (longest)


for m in range(len(tableData[0])):
for n in range(len(tableData)):
print (tableData[n][m],end=" ")
n+=1
print ("".rjust(lists['list:', 1],"-"))
m+=1


I'm almost done except for one thing, I can't make it right-justified. This output is the closest I came so far.



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


If I put rjust inside the inner for-loop the output is much different:



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









share|improve this question


















  • 1





    Have you tried joining first?

    – Ignacio Vazquez-Abrams
    Dec 28 '15 at 5:34











  • Could you explain a little bit more? Do you mean at the end of the print function or in the for loop?

    – Stanley Wilkins
    Dec 28 '15 at 6:50











  • I tried it now, but got argument error.

    – Stanley Wilkins
    Dec 28 '15 at 7:11














5












5








5


5






In this section, they want us to create this table:



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


It must be justified to the right, and the input is tableData. Here's my code:



tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
listlens=
tour=0
lists={}
for m in tableData:
total=0
tour+=1
for n in m:
total+=len(n)
lists["list:",tour]=total
print("list",tour,total)

itemcount=list(lists.values())
sortedlen=(sorted(itemcount,reverse=True))
longest=sortedlen[0]

#print (lists['list:', 1])
#print (longest)


for m in range(len(tableData[0])):
for n in range(len(tableData)):
print (tableData[n][m],end=" ")
n+=1
print ("".rjust(lists['list:', 1],"-"))
m+=1


I'm almost done except for one thing, I can't make it right-justified. This output is the closest I came so far.



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


If I put rjust inside the inner for-loop the output is much different:



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









share|improve this question














In this section, they want us to create this table:



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


It must be justified to the right, and the input is tableData. Here's my code:



tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
listlens=
tour=0
lists={}
for m in tableData:
total=0
tour+=1
for n in m:
total+=len(n)
lists["list:",tour]=total
print("list",tour,total)

itemcount=list(lists.values())
sortedlen=(sorted(itemcount,reverse=True))
longest=sortedlen[0]

#print (lists['list:', 1])
#print (longest)


for m in range(len(tableData[0])):
for n in range(len(tableData)):
print (tableData[n][m],end=" ")
n+=1
print ("".rjust(lists['list:', 1],"-"))
m+=1


I'm almost done except for one thing, I can't make it right-justified. This output is the closest I came so far.



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


If I put rjust inside the inner for-loop the output is much different:



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






python python-3.x printing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '15 at 5:28









Stanley WilkinsStanley Wilkins

6515




6515








  • 1





    Have you tried joining first?

    – Ignacio Vazquez-Abrams
    Dec 28 '15 at 5:34











  • Could you explain a little bit more? Do you mean at the end of the print function or in the for loop?

    – Stanley Wilkins
    Dec 28 '15 at 6:50











  • I tried it now, but got argument error.

    – Stanley Wilkins
    Dec 28 '15 at 7:11














  • 1





    Have you tried joining first?

    – Ignacio Vazquez-Abrams
    Dec 28 '15 at 5:34











  • Could you explain a little bit more? Do you mean at the end of the print function or in the for loop?

    – Stanley Wilkins
    Dec 28 '15 at 6:50











  • I tried it now, but got argument error.

    – Stanley Wilkins
    Dec 28 '15 at 7:11








1




1





Have you tried joining first?

– Ignacio Vazquez-Abrams
Dec 28 '15 at 5:34





Have you tried joining first?

– Ignacio Vazquez-Abrams
Dec 28 '15 at 5:34













Could you explain a little bit more? Do you mean at the end of the print function or in the for loop?

– Stanley Wilkins
Dec 28 '15 at 6:50





Could you explain a little bit more? Do you mean at the end of the print function or in the for loop?

– Stanley Wilkins
Dec 28 '15 at 6:50













I tried it now, but got argument error.

– Stanley Wilkins
Dec 28 '15 at 7:11





I tried it now, but got argument error.

– Stanley Wilkins
Dec 28 '15 at 7:11












30 Answers
30






active

oldest

votes


















3














Here's an alternate method that perhaps you could apply to your own code. I first took tableData and sorted it out into a dictionary so it's easier to work with. After that I found the longest list in terms of characters. This allows us to know how far over the shorter lists should go. Finally, I printed out each lists adding spaces in front of the shorter ones based on the difference from the longest.



# orginal data
tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

# empty dictonary for sorting the data
newTable = {0:, 1:, 2:, 3:}

# iterate through each list in tableData
for li in tableData:
for i in range(len(li)):
# put each item of tableData into newTable by index
newTable[i].append(li[i])

# determine the longest list by number of total characters
# for instance ['apples', 'Alice', 'dogs'] would be 15 characters
# we will start with longest being zero at the start
longest = 0
# iterate through newTable
# for example the first key:value will be 0:['apples', 'Alice', 'dogs']
# we only really care about the value (the list) in this case
for key, value in newTable.items():
# determine the total characters in each list
# so effectively len('applesAlicedogs') for the first list
length = len(''.join(value))
# if the length is the longest length so far,
# make that equal longest
if length > longest:
longest = length

# we will loop through the newTable one last time
# printing spaces infront of each list equal to the difference
# between the length of the longest list and length of the current list
# this way it's all nice and tidy to the right
for key, value in newTable.items():
print(' ' * (longest - len(''.join(value))) + ' '.join(value))





share|improve this answer


























  • Can we simplify _,li part somehow? I googled it and it looks very difficult, maybe even more than my question. Your approach is way more advanced than I could figure out by myself but if you would explain a little more it would help.

    – Stanley Wilkins
    Dec 28 '15 at 6:17











  • I simplified the code, and added comments so you can follow along. Hope this helps!

    – vesche
    Dec 28 '15 at 6:38











  • Thanks for making it easier, I love learning new ways to solve problems but since I'm at the very beginning, using zip() function and other built-in functions or methods to solve them would defeat the purpose of practicing the basics.

    – Stanley Wilkins
    Dec 28 '15 at 7:05











  • I didn't use zip. What built-in function are you having trouble understanding? I could rewrite something an easier way if you let me know what you're not getting.

    – vesche
    Dec 28 '15 at 7:09











  • No, I meant zip for the other answers. Your first code had some other advanced features but as for now it's very easy to understand.

    – Stanley Wilkins
    Dec 28 '15 at 7:13



















2














Here you go young padawan:



tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
maxlen = 0
for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
maxlen = max(len(fruit) + len (name) + len (animal), maxlen)
for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
length = len(fruit) + len (name) + len (animal)
print ((' ' * (maxlen - length)) + fruit, name, animal)


Looping to determine maxlen is probably not optimal, copypasting was just the quickest thing that came to my mind.






share|improve this answer


























  • You have a slight syntax error friend, should be print (' ' * (maxlen - length) + fruit, name, animal).

    – vesche
    Dec 28 '15 at 5:59











  • It works with vesche's correction to the last line, but about the fruit, name and animal, are they considered lists? It's my first time seeing built-in zip function, but I've done a little research about it now. Can we think it as a shortcut to make iterated lists from other lists?

    – Stanley Wilkins
    Dec 28 '15 at 6:37






  • 1





    Sorry for the error, very obviously i have my head still wrapped in python 2. @StanleyWilkins zip transforms multiple lists in a single list of tuples, which is indeed handy for some iterations. Fruit name and animal are the loop iterators, they are single values; the lists they are iterating are tableData[n].

    – Arthur Havlicek
    Dec 28 '15 at 10:14



















2














that's my method to solve this problem.



tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]


def printTable(mylist):
#getting the item who has the max length in the inner tables
maxLength = 0
for item in mylist:
for i in item:
if len(i) > maxLength:
maxLength = len(i)
else:
maxLength = maxLength
# make a seperated rjust for every item in the inner lists
for item in mylist:
for i in range(len(item)):
item[i] = (item[i].rjust(maxLength))
# convert list to dictionary data type it's more easier to deal with.
myNewlist = {0: , 1: , 2: , 3: }
for i in range(len(item)):
for u in tableData:
myNewlist[i].append(u[i])
# print the out put :)
for key, value in myNewlist.items():
print(''.join(value))


(printTable(tableData))





share|improve this answer

































    2














    This is how I did.



    For the first part of the code I just used the hint they give to us.



    In Chapter 4 / Practice Project / Character Picture Grid we've learned how to "rotate" and then print a list of lists. It was useful for the second part of my code.



    #!/usr/bin/python3
    # you can think of x and y as coordinates

    tableData = [['apples', 'oranges', 'cherries', 'banana'],
    ['Alice', 'Bob', 'Carol', 'David'],
    ['dogs', 'cats', 'moose', 'goose']]

    def printTable(table):
    # create a new list of 3 "0" values: one for each list in tableData
    colWidths = [0] * len(table)
    # search for the longest string in each list of tableData
    # and put the numbers of characters in the new list
    for y in range(len(table)):
    for x in table[y]:
    if colWidths[y] < len(x):
    colWidths[y] = len(x)

    # "rotate" and print the list of lists
    for x in range(len(table[0])) :
    for y in range(len(table)) :
    print(table[y][x].rjust(colWidths[y]), end = ' ')
    print()
    x += 1

    printTable(tableData)





    share|improve this answer
























    • thanks for this! my solution had me scratching my head. as far as I can tell, the 'x += 1' is unnecessary

      – kfrncs
      Oct 19 '18 at 16:12





















    1














    First join elements, then find the longest one and then you can use %*s to write lines. More in comments in code.



    tableData=[['apples', 'oranges', 'cherries', 'banana'],
    ['Alice', 'Bob', 'Carol', 'David'],
    ['dogs', 'cats', 'moose', 'goose']]

    longest = 0 # to find the longest line
    lines = # to keep lines

    for elements in zip(tableData[0], tableData[1], tableData[2]):

    # join elements in line - like 'apples' + ' ' + 'Alice' + ' ' + 'dogs'
    line = ' '.join(elements)

    # add line to the list
    lines.append(line)

    #print(line) # you can print it to see what you get

    # find the longest line
    length = len(line)
    if length > longest:
    longest = length

    #print('the longest:', longest)

    longest += 1 # to get one space more at left side

    # print lines using `%*s`
    # if `longest` is 21 then it will works as `%21s`
    for line in lines:
    print('%*s' % (longest, line))





    share|improve this answer

































      1














      Here is a solution. It works even if no.of inner lists changes or no.of elements in inner list changes given all inner lists have the same no.of elements.



      tableData = [
      ['apples', 'oranges', 'cherries', 'banana'],
      ['Alice', 'Bob', 'Carol', 'David'],
      ['dogs', 'cats', 'moose', 'goose']
      ]

      col_widths = list()
      for i, record in enumerate(tableData):
      col_widths.insert(i, max(len(item) for item in record))

      for i in range(len(tableData[0])):
      print(' '.join(record[i].rjust(col_widths[j]) for j, record in enumerate(tableData)))





      share|improve this answer
























      • Nice, short and clean answer!

        – Frieder
        Aug 31 '18 at 13:24



















      1














      I know it has been years, but i started reading the book couple of weeks ago and this is how figured out that one :'D



      tableData = [['apples', 'oranges', 'cherries', 'banana'], 
      ['Alice', 'Bob', 'Carol', 'David'],
      ['dogs', 'cats', 'moose', 'goose']]
      n=0
      x=''
      colWidths=[0]*len(tableData)

      for i in range(len(tableData)):
      for n in range(len(tableData[0])-1):
      if colWidths[i]<len(tableData[i][n])+1:
      colWidths[i]=len(tableData[i][n])+1

      for n in range(len(tableData[n])):
      x=''
      for i in range(len(tableData)):
      x+=str(tableData[i][n]).rjust(colWidths[i])

      print(x)





      share|improve this answer































        0














        I was having exactly the opposite problem: I had already figured out how to determine the parameter for right-justification, and how to right-justify the items. Yet I had difficulty printing several items in one single line. I tried the "end=''" but the output still looked strange. Eventually I tried to concatenate the items to be printed in one line and call the print function one more time in the loop. And it worked.



        It took me hours to do this simple exercise but it was definitely worth it!:) It feels really good looking back at how all the incremental improvements finally made the code work!



        Here is my code. Hopefully it will help.



        tableData = [['apples', 'oranges', 'cherries', 'banana'],
        ['Alice', 'Bob', 'Carol', 'David'],
        ['dogs', 'cats', 'moose', 'goose']]

        def printTable(tableData):
        colWidths = [0] * len(tableData)
        for i in range(len(tableData)):
        for j in range(len(tableData[i])):
        if colWidths[i] <= len(tableData[i][j]):
        colWidths[i] = len(tableData[i][j])
        else:
        colWidths[i] = colWidths[i]

        for j in range(len(tableData[i])):
        for i in range(len(tableData)):
        print(''.join(tableData[i][j].rjust(colWidths[i] + 1)), end = '')
        #the "+ 1" is used to allow for a space in between
        print()

        printTable(tableData)


        Btw, I was surprised that



        for j in range(len(tableData[i])):
        for i in range(len(tableData)):


        actually worked.



        Shouldn't one always use i before j in this case? It seemed counterintuitive to me yet when gave it a try anyway it miraculously worked.






        share|improve this answer































          0














          #! python3
          #table printer prints takes a list of lists of strings and displays it in a
          #well-organized table with each column right-justified.

          tableData = [['apples', 'oranges', 'cherries', 'banana'],
          ['Alice', 'Bob', 'Carol', 'David'],
          ['dogs', 'cats', 'moose', 'goose']]

          def printTable(data):
          #in this section we are creating a list containing each column's width
          colWidths = [0] * len(data)
          for m in range(len(colWidths)):
          for n in range(len(data[0])):
          if colWidths[m] < len(data[m][n]):
          colWidths[m] = len(data[m][n])
          #optionally you can also print colWidths for a better understanding
          #print(colWidths) will output [8, 5, 5]

          #this section of the code helps arranging the list in a table format
          for u in range(len(data[0])):
          for v in range(len(data)):
          print(data[v][u].rjust(colWidths[v] + 1), end='')
          print()

          printTable(tableData)





          share|improve this answer































            0














            Based on the author's hint:




            "Hint: Your code will first have to find the longest string in each of the inner lists so that the whole column can be wide enough to fit all the strings. You can store the maximum width of each column as a list of integers. The printTable() function can begin with colWidths = [0] * len(tableData), which will create a list containing the same number of 0 values as the number of inner lists in tableData. That way, colWidths[0] can store the width of the longest string in tableData[0], colWidths[1] can store the width of the longest string in tableData[1], and so on. You can then find the largest value in the colWidths list to find out what integer width to pass to the rjust() string method."




            Here is my answer:



            tableData = [['apples', 'oranges', 'cherries', 'banana'],
            ['Alice', 'Bob', 'Carol', 'David'],
            ['dogs', 'cats', 'moose', 'goose']]


            def table_printer(tab_data):
            col_widths = [0] * len(tab_data) # creates 3 lists based on the list length
            for j in range(len(tab_data[0])): # finds a length of 4 items (aka rows)
            for i in range(len(tab_data)): # finds a length of 3 items (aka columns)
            col_widths[i] = len((max(tab_data[i], key=len))) # sets the column width to the maximum length of an item in the list
            a = tab_data[i][j]
            print(a.rjust(col_widths[i]), end=" ") # every time we print a column, we rjust it to the max width.
            print("n")


            table_printer(tableData)





            share|improve this answer

































              0














              So this is what I ended up with..without too much internet help. That print line sucks however. I liked some of the above but wasn't going to copycat.



              tableData = [['apples','oranges','cherries','banana'],
              ['Alice','Bob','Carol','David'],
              ['dogs','cats','moose','goose']]

              def printTable():
              colWidths=[0]*len(tableData)
              for i in range(len(tableData)):
              for x in range(len(tableData[i])):
              if colWidths[i]<len(tableData[i][x]):
              colWidths[i]=len(tableData[i][x])
              for x in range(len(tableData[i])):
              print(tableData[0][x].rjust(colWidths[0]+1) + tableData[1][x].rjust(colWidths[1]+1) + tableData[2][x].rjust(colWidths[2]+1))

              printTable()


              The print comes out correct, but I do not like how it doesn't allow for a dynamic use. Back to the drawing board on the print line.






              share|improve this answer

































                0














                Here's how I did it, using both the hint and only information used in the book so far.



                This code works no matter how many sublists are within tableData, and no matter how many items are in each sublist.



                I used a loop within a loop to achieve this, and print a space after each printed item. If it's the last category item, then print a new line.



                tableData = [['apples', 'oranges', 'cherries', 'banana','orange'],
                ['Alice', 'Bob', 'Carol', 'David','Phillip'],
                ['dogs', 'cats', 'moose', 'goose','anteater'],
                ['mitsubishi','honda','toyota','ford','range rover']]


                def printTable(table):
                colWidths = [0] * len(table)
                for i in range(len(table)):
                for x in table[i]:
                if len(x) > colWidths[i]:
                colWidths[i] = len(x)
                print(colWidths)

                for i in range(len(table[0])):
                for x in range(len(table)):
                print(table[x][i].rjust(colWidths[x]),end = ' ')
                if x == len(table)-1:
                print('r')



                printTable(tableData)


                '''
                table[0,0] + table [1,0] + table [2,0]
                table[1,0] + table [1,1]

                '''





                share|improve this answer

































                  0














                  So many different solutions! The book teaches us to each range(len(x)) and I've read online that this is not a good way of getting indexes. A better suggested solution is enumerate which I used in my code, you can find more information here:



                  https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence



                  #! python3
                  # printTable.py - Displays a list in a well organized table

                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                  ['Alice', 'Bob', 'Carol', 'David'],
                  ['dogs', 'cats', 'moose', 'goose']]
                  columnWidth = # Creates a empty list

                  def printtable(printdata):
                  for data in printdata:
                  # Adds the length of the longest string to the columnWidth list
                  columnWidth.append(len(max(data, key=len)))
                  # For loop to decide the determine the number of columns to cycle through
                  for x, columnData in enumerate(printdata[0]):
                  # For loop for the number of rows to cycle through
                  for y, rowData in enumerate(printdata):
                  # Print each row data with the correct justification
                  print(printdata[y][x].rjust(columnWidth[y]), end=' ')
                  # Create a new line before reiterating
                  print('')

                  printtable(tableData)





                  share|improve this answer
























                  • there is already an answer using enumeration: stackoverflow.com/a/38292570/2648551

                    – colidyre
                    Aug 30 '18 at 14:08



















                  0














                  I think this would be the answer if you follow the book. Tips and what you have learned so far included.



                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                  ['Alice', 'Bob', 'Carol', 'David'],
                  ['dogs', 'cats', 'moose', 'goose']]


                  Create the function 'printTable'. First thing is getting the length of the 3 longest string in the 3 lists and putting the integer values in de list 'colWidths'



                  def printTable(table):
                  colWidths = [0] * len(table) # The tip from the book
                  for i in range(len(table)):
                  for s in range(len(table[i])):
                  l = len(table[i][s]) # get the length of the individual strings
                  if colWidths[i] < l: # check wich one is the longest
                  colWidths[i] = l # save the value in the list


                  Next part of the function is getting the right column oirder from the items in the list. I've had some troubl;e with this part, but eventually i've got it.



                      for x in range(len(table[0])):
                  for y in range(len(table)):
                  if y == len(table) - 1:
                  print(table[y][x].rjust(colWidths[y], ' '))
                  else:
                  print(table[y][x].rjust(colWidths[y], ' '), end=' ')


                  execute the function:



                  printTable(tableData)





                  share|improve this answer































                    -1














                    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                    ['Alice', 'Bob', 'Carol', 'David'],
                    ['dogs', 'cats', 'moose', 'goose']]

                    def printTable(data):
                    for j in range(len(data[0])):
                    for i in range(len(data)):
                    #This line finds the longest item for each list and its length
                    x = len(max(data[i], key=len))
                    print(data[i][j].rjust(x), end=' ')
                    print()

                    printTable(tableData)





                    share|improve this answer































                      -1














                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                      ['Alice', 'Bob', 'Carol', 'David'],
                      ['dogs', 'cats', 'moose', 'goose']]



                      def printTable():
                      #List colWidth contains the longest string in each of the inner lists
                      colWidth=[0]*len(tableData)

                      n=0
                      #To find the longest string in each of the inner lists and store in
                      colWidth
                      for li in tableData:
                      num=0
                      for j in li:
                      if(num<len(j)):
                      num=len(j)
                      colWidth[n]=num
                      n=n+1

                      #To find the largest value in the colWidths list to find out what integer
                      width to pass to the rjust() string method.
                      c=0
                      for i in colWidth:
                      if(c<i):
                      c=i

                      #To print the data
                      for m in range(len(tableData[0])):
                      for n in range(len(tableData)):
                      print (tableData[n][m]).rjust(c),
                      print('')

                      printTable()





                      share|improve this answer































                        -1














                        tableData = [['apples', 'oranges', 'cherries', 'banana'],
                        ['Alice', 'Bob', 'Carol', 'David'],
                        ['dogs', 'cats', 'moose', 'goose']]

                        def printTable(list):
                        len_list =
                        for i in range(len(list)):
                        len_list.append(len(max(list[i], key=len)))
                        for m in range(len(list[i])):
                        for i in range(len(list)):
                        print(list[i][m].rjust(len_list[i]+1), end = "")
                        print() #to add a new line

                        printTable(tableData)





                        share|improve this answer































                          -1














                          I think the easiest solution is to find the length of maximum size string in the whole list(inner and outter) and then set it as argument for right justification method (rjust()) then use loops for printing the list values according to the question.



                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                          ['Alice', 'Bob', 'Carol', 'David'],
                          ['dogs', 'cats', 'moose', 'goose']]


                          innerlen=0

                          for m in tableData:
                          for n in m:
                          if innerlen < len(n):
                          innerlen = len(n)




                          for m in range(len(tableData[0])):
                          for n in range(len(tableData)):
                          print(tableData[n][m].rjust(innerlen),end="")

                          print("")





                          share|improve this answer































                            -1














                            Here is my approach to the exercise:



                            #!/usr/bin/env python3

                            tableData = [['apples', 'oranges', 'cherries', 'banana'],
                            ['Alice', 'Bob', 'Carol', 'David'],
                            ['dogs', 'cats', 'moose','goose']]

                            def printTable():
                            colWidths = [0] * len(tableData)

                            # find longest word in each list, convert to int
                            # and add to colWidths var
                            for i in range(len(tableData)):
                            for l in tableData[i]:
                            if len(l) >= colWidths[i]:
                            colWidths[i] = len(l)
                            # print and justify using the values from colWidths + 1
                            for t in range(4):
                            print(tableData[0][t].rjust(colWidths[0]+1) +
                            tableData[1][t].rjust(colWidths[1]+1) +
                            tableData[2][t].rjust(colWidths[2]+1))

                            printTable()





                            share|improve this answer































                              -1














                              def print_table(tab):
                              for j in range(len(tab[0])):
                              for i in range(len(tab)):
                              m = max([len(s) for s in tab[i]])
                              print(tab[i][j].rjust(m), end=' ')
                              print('')


                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                              ['Alice', 'Bob', 'Carol', 'David'],
                              ['dogs', 'cats', 'moose', 'goose']]

                              print_table(tableData)





                              share|improve this answer































                                -1














                                Maybe not the best way, but here is my solution to the task:



                                def printtable(listlist):
                                # variable stores the maximum length of the words in the lists
                                lenghtCounter = 0 #8
                                listCounter = 0 #3
                                dict = {}

                                for list in listlist:
                                listCounter += 1
                                wordcounter = 0

                                for pos in range(len(list)):
                                wordcounter += 1

                                for word in list[pos:len(list):len(list)]:
                                dict.update({list[pos]: pos})

                                # length counter will store the longest value
                                if len(word) > lenghtCounter:
                                lenghtCounter = len(word)

                                for i in range(wordcounter):
                                line =
                                strline = ''

                                for k, v in dict.items():
                                if v == i:
                                line.append(k)
                                strline.join(k.ljust(lenghtCounter))

                                for el in line:
                                strline += el.ljust(lenghtCounter + 5)
                                print(strline)

                                tableData = [
                                ['apples', 'oranges', 'cherries', 'bananas'],
                                ['Alice', 'Bob', 'Carol', 'David'],
                                ['dogs', 'cats', 'moose', 'goose']
                                ]

                                printtable(tableData)





                                share|improve this answer

































                                  -1














                                  There you go



                                  tableData = [['apples', 'oranges', 'cherries','mango'],
                                  ['Alice', 'Bob', 'Carol', 'David'],
                                  ['dogs', 'cats', 'moose', 'goose']]

                                  def printTable(table):
                                  colWid=[0]*len(tableData)
                                  i=0
                                  for row in tableData:
                                  colWid[i]=len(max(row, key=len))
                                  i+=1



                                  print(colWid)

                                  wid=max(colWid)


                                  for m in range(len(tableData[0])):

                                  for n in range(len(tableData)):

                                  print (tableData[n][m].ljust(wid),end=' ')

                                  print()

                                  printTable(tableData)





                                  share|improve this answer
























                                  • Please don't just dump your code here, write a good description for your answer. Please refer to: stackoverflow.com/help/how-to-answer

                                    – sɐunıɔןɐqɐp
                                    May 6 '18 at 14:56



















                                  -1














                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                  ['Alice', 'Bob', 'Carol', 'David'],
                                  ['dogs', 'cats', 'moose', 'goose']]



                                  def find_max_length(item_list):
                                  #find the length of the "item_list" parameter
                                  colWidth = [0] * len(item_list)

                                  #an empty list created to hold a single inner list from the #"item_list" parameter
                                  not_so_emptylist =

                                  i = 0
                                  maxlength = 0 #variable to hold max length of an item in the inner list

                                  for i in range(len(item_list)):
                                  not_so_emptylist = item_list[i]
                                  for item in not_so_emptylist:
                                  if len(item) > maxlength:
                                  maxlength = len(item)
                                  colWidth[i] = maxlength
                                  maxlength = 0

                                  return colWidth

                                  #an empty list to pass colwidth to
                                  width =

                                  def print_table_data(a_list):
                                  width = find_max_length(a_list)

                                  i = 0

                                  for i in range(4):
                                  print(a_list[0][i].rjust(width[0]) + ' ' + a_list[1][i].rjust(width[1]) + ' ' + a_list[2][i].rjust(width[2]))

                                  print_table_data(a_list=tableData)





                                  share|improve this answer
























                                  • Explaining your solution can be really helpful.

                                    – onetwo12
                                    May 7 '18 at 14:27



















                                  -1














                                  Here, first of all we have to calculate the length of longest string in each of inner list, which we will store in the "colWidths" list. After that we will simply traverse through the "tableData" list. But while printing, we need to right justify each string by maximum column width (i.e. stored in colwidth) for that string so that symmetry can be maintained.Else is just printing.



                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                  ['Alice', 'Bob', 'Carol', 'David'],
                                  ['dogs', 'cats', 'moose', 'goose']]

                                  def printTable(t):
                                  colWidths=[0] * len(tableData)
                                  l=
                                  for j in range(len(t)):
                                  for i in t[j]:
                                  l+=[len(i)]
                                  colWidths[j]= max(l)
                                  l=
                                  print(colWidths)

                                  for j in range(len(t[0])):
                                  for i in range(len(t)):
                                  print(t[i][j].rjust(colWidths[i]),end=' ')
                                  print(end='n')

                                  printTable(tableData)





                                  share|improve this answer

































                                    -1














                                    def table_print(tabledata):
                                    column=[0]*len(tabledata)
                                    for k in range(len(tabledata)):
                                    column[k]=len(max(tabledata[k],key=len))

                                    for i in range(len(tabledata[0])):
                                    for j in range(len(tabledata)):
                                    print(tabledata[j][i].rjust(column[j]+1),end="")
                                    print()
                                    return
                                    table_Data = [['app', 'oranges', 'cherries', 'banana'],
                                    ['Ale', 'Bob', 'Crol', 'Dad'],
                                    ['dogs', 'cats', 'moose', 'ge']]
                                    table_print(table_Data)





                                    share|improve this answer
























                                    • Welcome to SO. I am sure you can add some descriptions and gotchas in the code, to make a better answer.

                                      – Nilay Vishwakarma
                                      Jun 11 '18 at 19:36



















                                    -1














                                    It's fun to see how everyone does it differently but still gets the same result. I did it like this:



                                    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                    ['Alice', 'Bob', 'Carol', 'David'],
                                    ['dogs', 'cats', 'moose', 'goose']]

                                    def printTable(table):
                                    table_len =
                                    max_of_table =
                                    next_item = ''
                                    for i in range(len(table)):
                                    temp_len =
                                    for k in range(len(table[i])):
                                    temp_len.append(len(table[i][k]))
                                    table_len.append(temp_len)
                                    for b in table_len:
                                    max_of_table.append(max(b))
                                    for a in range(len(table[0])):
                                    for s in range(len(table)):
                                    next_item = str(table[s][a])
                                    next_item = next_item.rjust(max_of_table[s])
                                    print(next_item, end=' ')
                                    print('')

                                    printTable(tableData)





                                    share|improve this answer































                                      -1














                                      My solution:



                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                      ['Alice', 'Bob', 'Carol', 'David'],
                                      ['dogs', 'cats', 'moose', 'goose']]


                                      def printTable(table):
                                      liste = 0
                                      colWidths = [0] * len(tableData)
                                      for lister in table:
                                      liste = liste +1
                                      longest =0
                                      for strenge in lister:
                                      if len(strenge) > longest:
                                      longest = len(strenge)
                                      colWidths.insert((liste-1),longest)

                                      for i in range(len(lister)):
                                      print()
                                      for lister in table:
                                      print (lister[i].rjust(colWidths[0]),end='')

                                      printTable(tableData)





                                      share|improve this answer































                                        -1














                                        This is the simplest way I found to satisfy the goal. Here is my code:



                                        tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                        ['Alice', 'Bob', 'Carol', 'David'],
                                        ['dogs', 'cats', 'moose', 'goose']]

                                        def printTable(myTable):

                                        #each column is a different width, so colWidth is a list which contains the width of each column
                                        #this is also suggested in the problem itself
                                        colWidth=[0]*len(myTable)
                                        for x in range(len(myTable)):
                                        for item in myTable[x]:
                                        if len(item)>colWidth[x]:
                                        colWidth[x]=len(item)

                                        #printing the table is similar to the exercise at the end of Chapter 4
                                        for a in range (len(myTable[0])):
                                        for b in range (len(myTable)):
                                        print (str(myTable[b][a]).rjust(colWidth[b]), end = ' ')
                                        print('n')

                                        printTable(tableData)





                                        share|improve this answer

































                                          -1














                                          # table printer
                                          def printtable(tabledata,columnwidth):
                                          for y in range(4):
                                          print()
                                          for x in range(len(tabledata)):
                                          print(tabledata[x][y].rjust(columnwidth),end='')
                                          tabledata=[['apples','oranges','cherries','banana'],.
                                          ['Alice','Bob','Carol','David'],
                                          ['dogs','cats','moose','goose']]
                                          n=len(tabledata[0][0]
                                          for y in range(4):
                                          for x in range(len(tabledata)):
                                          if len(tabledata[0][0])>=len(tabledata[x][y]):
                                          False
                                          else:
                                          n=len(tabledata[x][y])
                                          printtable(tabledata,n)





                                          share|improve this answer


























                                          • Welcome to stack overflow. Please also add a description on what the code does instead of just providing the code.

                                            – Sashi
                                            Jan 1 at 6:52



















                                          -2














                                          #! python3
                                          # Table Printer 1

                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]

                                          def printTable(data):
                                          colWidths = [0] * len(data)
                                          for y in range(len(data[0])):
                                          for x in range(len(data)):
                                          colWidths[x] = len(max(data[x], key = len))
                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                          print()

                                          printTable(tableData)


                                          #! python3
                                          # Table Printer 2

                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]

                                          def printTable(data):
                                          colWidths = [0] * len(data)
                                          for x in range(len(data)):
                                          for y in range(len(data[0])):
                                          if len(data[x][y]) > colWidths[x]:
                                          colWidths[x] = len(data[x][y])
                                          for y in range(len(data[0])):
                                          for x in range(len(data)):
                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                          print()

                                          printTable(tableData)





                                          share|improve this answer



















                                          • 1





                                            Hi test, could you develop what you're doing here to answer the question? It's absolutely not clear.

                                            – J. Chomel
                                            May 30 '16 at 7:32






                                          • 1





                                            Could you please add some context to your code?

                                            – ppperry
                                            May 30 '16 at 13:38










                                          protected by Community Jan 1 at 6:41



                                          Thank you for your interest in this question.
                                          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                          Would you like to answer one of these unanswered questions instead?














                                          30 Answers
                                          30






                                          active

                                          oldest

                                          votes








                                          30 Answers
                                          30






                                          active

                                          oldest

                                          votes









                                          active

                                          oldest

                                          votes






                                          active

                                          oldest

                                          votes









                                          3














                                          Here's an alternate method that perhaps you could apply to your own code. I first took tableData and sorted it out into a dictionary so it's easier to work with. After that I found the longest list in terms of characters. This allows us to know how far over the shorter lists should go. Finally, I printed out each lists adding spaces in front of the shorter ones based on the difference from the longest.



                                          # orginal data
                                          tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]

                                          # empty dictonary for sorting the data
                                          newTable = {0:, 1:, 2:, 3:}

                                          # iterate through each list in tableData
                                          for li in tableData:
                                          for i in range(len(li)):
                                          # put each item of tableData into newTable by index
                                          newTable[i].append(li[i])

                                          # determine the longest list by number of total characters
                                          # for instance ['apples', 'Alice', 'dogs'] would be 15 characters
                                          # we will start with longest being zero at the start
                                          longest = 0
                                          # iterate through newTable
                                          # for example the first key:value will be 0:['apples', 'Alice', 'dogs']
                                          # we only really care about the value (the list) in this case
                                          for key, value in newTable.items():
                                          # determine the total characters in each list
                                          # so effectively len('applesAlicedogs') for the first list
                                          length = len(''.join(value))
                                          # if the length is the longest length so far,
                                          # make that equal longest
                                          if length > longest:
                                          longest = length

                                          # we will loop through the newTable one last time
                                          # printing spaces infront of each list equal to the difference
                                          # between the length of the longest list and length of the current list
                                          # this way it's all nice and tidy to the right
                                          for key, value in newTable.items():
                                          print(' ' * (longest - len(''.join(value))) + ' '.join(value))





                                          share|improve this answer


























                                          • Can we simplify _,li part somehow? I googled it and it looks very difficult, maybe even more than my question. Your approach is way more advanced than I could figure out by myself but if you would explain a little more it would help.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 6:17











                                          • I simplified the code, and added comments so you can follow along. Hope this helps!

                                            – vesche
                                            Dec 28 '15 at 6:38











                                          • Thanks for making it easier, I love learning new ways to solve problems but since I'm at the very beginning, using zip() function and other built-in functions or methods to solve them would defeat the purpose of practicing the basics.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 7:05











                                          • I didn't use zip. What built-in function are you having trouble understanding? I could rewrite something an easier way if you let me know what you're not getting.

                                            – vesche
                                            Dec 28 '15 at 7:09











                                          • No, I meant zip for the other answers. Your first code had some other advanced features but as for now it's very easy to understand.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 7:13
















                                          3














                                          Here's an alternate method that perhaps you could apply to your own code. I first took tableData and sorted it out into a dictionary so it's easier to work with. After that I found the longest list in terms of characters. This allows us to know how far over the shorter lists should go. Finally, I printed out each lists adding spaces in front of the shorter ones based on the difference from the longest.



                                          # orginal data
                                          tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]

                                          # empty dictonary for sorting the data
                                          newTable = {0:, 1:, 2:, 3:}

                                          # iterate through each list in tableData
                                          for li in tableData:
                                          for i in range(len(li)):
                                          # put each item of tableData into newTable by index
                                          newTable[i].append(li[i])

                                          # determine the longest list by number of total characters
                                          # for instance ['apples', 'Alice', 'dogs'] would be 15 characters
                                          # we will start with longest being zero at the start
                                          longest = 0
                                          # iterate through newTable
                                          # for example the first key:value will be 0:['apples', 'Alice', 'dogs']
                                          # we only really care about the value (the list) in this case
                                          for key, value in newTable.items():
                                          # determine the total characters in each list
                                          # so effectively len('applesAlicedogs') for the first list
                                          length = len(''.join(value))
                                          # if the length is the longest length so far,
                                          # make that equal longest
                                          if length > longest:
                                          longest = length

                                          # we will loop through the newTable one last time
                                          # printing spaces infront of each list equal to the difference
                                          # between the length of the longest list and length of the current list
                                          # this way it's all nice and tidy to the right
                                          for key, value in newTable.items():
                                          print(' ' * (longest - len(''.join(value))) + ' '.join(value))





                                          share|improve this answer


























                                          • Can we simplify _,li part somehow? I googled it and it looks very difficult, maybe even more than my question. Your approach is way more advanced than I could figure out by myself but if you would explain a little more it would help.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 6:17











                                          • I simplified the code, and added comments so you can follow along. Hope this helps!

                                            – vesche
                                            Dec 28 '15 at 6:38











                                          • Thanks for making it easier, I love learning new ways to solve problems but since I'm at the very beginning, using zip() function and other built-in functions or methods to solve them would defeat the purpose of practicing the basics.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 7:05











                                          • I didn't use zip. What built-in function are you having trouble understanding? I could rewrite something an easier way if you let me know what you're not getting.

                                            – vesche
                                            Dec 28 '15 at 7:09











                                          • No, I meant zip for the other answers. Your first code had some other advanced features but as for now it's very easy to understand.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 7:13














                                          3












                                          3








                                          3







                                          Here's an alternate method that perhaps you could apply to your own code. I first took tableData and sorted it out into a dictionary so it's easier to work with. After that I found the longest list in terms of characters. This allows us to know how far over the shorter lists should go. Finally, I printed out each lists adding spaces in front of the shorter ones based on the difference from the longest.



                                          # orginal data
                                          tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]

                                          # empty dictonary for sorting the data
                                          newTable = {0:, 1:, 2:, 3:}

                                          # iterate through each list in tableData
                                          for li in tableData:
                                          for i in range(len(li)):
                                          # put each item of tableData into newTable by index
                                          newTable[i].append(li[i])

                                          # determine the longest list by number of total characters
                                          # for instance ['apples', 'Alice', 'dogs'] would be 15 characters
                                          # we will start with longest being zero at the start
                                          longest = 0
                                          # iterate through newTable
                                          # for example the first key:value will be 0:['apples', 'Alice', 'dogs']
                                          # we only really care about the value (the list) in this case
                                          for key, value in newTable.items():
                                          # determine the total characters in each list
                                          # so effectively len('applesAlicedogs') for the first list
                                          length = len(''.join(value))
                                          # if the length is the longest length so far,
                                          # make that equal longest
                                          if length > longest:
                                          longest = length

                                          # we will loop through the newTable one last time
                                          # printing spaces infront of each list equal to the difference
                                          # between the length of the longest list and length of the current list
                                          # this way it's all nice and tidy to the right
                                          for key, value in newTable.items():
                                          print(' ' * (longest - len(''.join(value))) + ' '.join(value))





                                          share|improve this answer















                                          Here's an alternate method that perhaps you could apply to your own code. I first took tableData and sorted it out into a dictionary so it's easier to work with. After that I found the longest list in terms of characters. This allows us to know how far over the shorter lists should go. Finally, I printed out each lists adding spaces in front of the shorter ones based on the difference from the longest.



                                          # orginal data
                                          tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]

                                          # empty dictonary for sorting the data
                                          newTable = {0:, 1:, 2:, 3:}

                                          # iterate through each list in tableData
                                          for li in tableData:
                                          for i in range(len(li)):
                                          # put each item of tableData into newTable by index
                                          newTable[i].append(li[i])

                                          # determine the longest list by number of total characters
                                          # for instance ['apples', 'Alice', 'dogs'] would be 15 characters
                                          # we will start with longest being zero at the start
                                          longest = 0
                                          # iterate through newTable
                                          # for example the first key:value will be 0:['apples', 'Alice', 'dogs']
                                          # we only really care about the value (the list) in this case
                                          for key, value in newTable.items():
                                          # determine the total characters in each list
                                          # so effectively len('applesAlicedogs') for the first list
                                          length = len(''.join(value))
                                          # if the length is the longest length so far,
                                          # make that equal longest
                                          if length > longest:
                                          longest = length

                                          # we will loop through the newTable one last time
                                          # printing spaces infront of each list equal to the difference
                                          # between the length of the longest list and length of the current list
                                          # this way it's all nice and tidy to the right
                                          for key, value in newTable.items():
                                          print(' ' * (longest - len(''.join(value))) + ' '.join(value))






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Dec 28 '15 at 6:37

























                                          answered Dec 28 '15 at 5:53









                                          veschevesche

                                          1,144614




                                          1,144614













                                          • Can we simplify _,li part somehow? I googled it and it looks very difficult, maybe even more than my question. Your approach is way more advanced than I could figure out by myself but if you would explain a little more it would help.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 6:17











                                          • I simplified the code, and added comments so you can follow along. Hope this helps!

                                            – vesche
                                            Dec 28 '15 at 6:38











                                          • Thanks for making it easier, I love learning new ways to solve problems but since I'm at the very beginning, using zip() function and other built-in functions or methods to solve them would defeat the purpose of practicing the basics.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 7:05











                                          • I didn't use zip. What built-in function are you having trouble understanding? I could rewrite something an easier way if you let me know what you're not getting.

                                            – vesche
                                            Dec 28 '15 at 7:09











                                          • No, I meant zip for the other answers. Your first code had some other advanced features but as for now it's very easy to understand.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 7:13



















                                          • Can we simplify _,li part somehow? I googled it and it looks very difficult, maybe even more than my question. Your approach is way more advanced than I could figure out by myself but if you would explain a little more it would help.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 6:17











                                          • I simplified the code, and added comments so you can follow along. Hope this helps!

                                            – vesche
                                            Dec 28 '15 at 6:38











                                          • Thanks for making it easier, I love learning new ways to solve problems but since I'm at the very beginning, using zip() function and other built-in functions or methods to solve them would defeat the purpose of practicing the basics.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 7:05











                                          • I didn't use zip. What built-in function are you having trouble understanding? I could rewrite something an easier way if you let me know what you're not getting.

                                            – vesche
                                            Dec 28 '15 at 7:09











                                          • No, I meant zip for the other answers. Your first code had some other advanced features but as for now it's very easy to understand.

                                            – Stanley Wilkins
                                            Dec 28 '15 at 7:13

















                                          Can we simplify _,li part somehow? I googled it and it looks very difficult, maybe even more than my question. Your approach is way more advanced than I could figure out by myself but if you would explain a little more it would help.

                                          – Stanley Wilkins
                                          Dec 28 '15 at 6:17





                                          Can we simplify _,li part somehow? I googled it and it looks very difficult, maybe even more than my question. Your approach is way more advanced than I could figure out by myself but if you would explain a little more it would help.

                                          – Stanley Wilkins
                                          Dec 28 '15 at 6:17













                                          I simplified the code, and added comments so you can follow along. Hope this helps!

                                          – vesche
                                          Dec 28 '15 at 6:38





                                          I simplified the code, and added comments so you can follow along. Hope this helps!

                                          – vesche
                                          Dec 28 '15 at 6:38













                                          Thanks for making it easier, I love learning new ways to solve problems but since I'm at the very beginning, using zip() function and other built-in functions or methods to solve them would defeat the purpose of practicing the basics.

                                          – Stanley Wilkins
                                          Dec 28 '15 at 7:05





                                          Thanks for making it easier, I love learning new ways to solve problems but since I'm at the very beginning, using zip() function and other built-in functions or methods to solve them would defeat the purpose of practicing the basics.

                                          – Stanley Wilkins
                                          Dec 28 '15 at 7:05













                                          I didn't use zip. What built-in function are you having trouble understanding? I could rewrite something an easier way if you let me know what you're not getting.

                                          – vesche
                                          Dec 28 '15 at 7:09





                                          I didn't use zip. What built-in function are you having trouble understanding? I could rewrite something an easier way if you let me know what you're not getting.

                                          – vesche
                                          Dec 28 '15 at 7:09













                                          No, I meant zip for the other answers. Your first code had some other advanced features but as for now it's very easy to understand.

                                          – Stanley Wilkins
                                          Dec 28 '15 at 7:13





                                          No, I meant zip for the other answers. Your first code had some other advanced features but as for now it's very easy to understand.

                                          – Stanley Wilkins
                                          Dec 28 '15 at 7:13













                                          2














                                          Here you go young padawan:



                                          tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]
                                          maxlen = 0
                                          for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
                                          maxlen = max(len(fruit) + len (name) + len (animal), maxlen)
                                          for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
                                          length = len(fruit) + len (name) + len (animal)
                                          print ((' ' * (maxlen - length)) + fruit, name, animal)


                                          Looping to determine maxlen is probably not optimal, copypasting was just the quickest thing that came to my mind.






                                          share|improve this answer


























                                          • You have a slight syntax error friend, should be print (' ' * (maxlen - length) + fruit, name, animal).

                                            – vesche
                                            Dec 28 '15 at 5:59











                                          • It works with vesche's correction to the last line, but about the fruit, name and animal, are they considered lists? It's my first time seeing built-in zip function, but I've done a little research about it now. Can we think it as a shortcut to make iterated lists from other lists?

                                            – Stanley Wilkins
                                            Dec 28 '15 at 6:37






                                          • 1





                                            Sorry for the error, very obviously i have my head still wrapped in python 2. @StanleyWilkins zip transforms multiple lists in a single list of tuples, which is indeed handy for some iterations. Fruit name and animal are the loop iterators, they are single values; the lists they are iterating are tableData[n].

                                            – Arthur Havlicek
                                            Dec 28 '15 at 10:14
















                                          2














                                          Here you go young padawan:



                                          tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]
                                          maxlen = 0
                                          for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
                                          maxlen = max(len(fruit) + len (name) + len (animal), maxlen)
                                          for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
                                          length = len(fruit) + len (name) + len (animal)
                                          print ((' ' * (maxlen - length)) + fruit, name, animal)


                                          Looping to determine maxlen is probably not optimal, copypasting was just the quickest thing that came to my mind.






                                          share|improve this answer


























                                          • You have a slight syntax error friend, should be print (' ' * (maxlen - length) + fruit, name, animal).

                                            – vesche
                                            Dec 28 '15 at 5:59











                                          • It works with vesche's correction to the last line, but about the fruit, name and animal, are they considered lists? It's my first time seeing built-in zip function, but I've done a little research about it now. Can we think it as a shortcut to make iterated lists from other lists?

                                            – Stanley Wilkins
                                            Dec 28 '15 at 6:37






                                          • 1





                                            Sorry for the error, very obviously i have my head still wrapped in python 2. @StanleyWilkins zip transforms multiple lists in a single list of tuples, which is indeed handy for some iterations. Fruit name and animal are the loop iterators, they are single values; the lists they are iterating are tableData[n].

                                            – Arthur Havlicek
                                            Dec 28 '15 at 10:14














                                          2












                                          2








                                          2







                                          Here you go young padawan:



                                          tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]
                                          maxlen = 0
                                          for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
                                          maxlen = max(len(fruit) + len (name) + len (animal), maxlen)
                                          for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
                                          length = len(fruit) + len (name) + len (animal)
                                          print ((' ' * (maxlen - length)) + fruit, name, animal)


                                          Looping to determine maxlen is probably not optimal, copypasting was just the quickest thing that came to my mind.






                                          share|improve this answer















                                          Here you go young padawan:



                                          tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]
                                          maxlen = 0
                                          for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
                                          maxlen = max(len(fruit) + len (name) + len (animal), maxlen)
                                          for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
                                          length = len(fruit) + len (name) + len (animal)
                                          print ((' ' * (maxlen - length)) + fruit, name, animal)


                                          Looping to determine maxlen is probably not optimal, copypasting was just the quickest thing that came to my mind.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Dec 28 '15 at 10:03

























                                          answered Dec 28 '15 at 5:44









                                          Arthur HavlicekArthur Havlicek

                                          777411




                                          777411













                                          • You have a slight syntax error friend, should be print (' ' * (maxlen - length) + fruit, name, animal).

                                            – vesche
                                            Dec 28 '15 at 5:59











                                          • It works with vesche's correction to the last line, but about the fruit, name and animal, are they considered lists? It's my first time seeing built-in zip function, but I've done a little research about it now. Can we think it as a shortcut to make iterated lists from other lists?

                                            – Stanley Wilkins
                                            Dec 28 '15 at 6:37






                                          • 1





                                            Sorry for the error, very obviously i have my head still wrapped in python 2. @StanleyWilkins zip transforms multiple lists in a single list of tuples, which is indeed handy for some iterations. Fruit name and animal are the loop iterators, they are single values; the lists they are iterating are tableData[n].

                                            – Arthur Havlicek
                                            Dec 28 '15 at 10:14



















                                          • You have a slight syntax error friend, should be print (' ' * (maxlen - length) + fruit, name, animal).

                                            – vesche
                                            Dec 28 '15 at 5:59











                                          • It works with vesche's correction to the last line, but about the fruit, name and animal, are they considered lists? It's my first time seeing built-in zip function, but I've done a little research about it now. Can we think it as a shortcut to make iterated lists from other lists?

                                            – Stanley Wilkins
                                            Dec 28 '15 at 6:37






                                          • 1





                                            Sorry for the error, very obviously i have my head still wrapped in python 2. @StanleyWilkins zip transforms multiple lists in a single list of tuples, which is indeed handy for some iterations. Fruit name and animal are the loop iterators, they are single values; the lists they are iterating are tableData[n].

                                            – Arthur Havlicek
                                            Dec 28 '15 at 10:14

















                                          You have a slight syntax error friend, should be print (' ' * (maxlen - length) + fruit, name, animal).

                                          – vesche
                                          Dec 28 '15 at 5:59





                                          You have a slight syntax error friend, should be print (' ' * (maxlen - length) + fruit, name, animal).

                                          – vesche
                                          Dec 28 '15 at 5:59













                                          It works with vesche's correction to the last line, but about the fruit, name and animal, are they considered lists? It's my first time seeing built-in zip function, but I've done a little research about it now. Can we think it as a shortcut to make iterated lists from other lists?

                                          – Stanley Wilkins
                                          Dec 28 '15 at 6:37





                                          It works with vesche's correction to the last line, but about the fruit, name and animal, are they considered lists? It's my first time seeing built-in zip function, but I've done a little research about it now. Can we think it as a shortcut to make iterated lists from other lists?

                                          – Stanley Wilkins
                                          Dec 28 '15 at 6:37




                                          1




                                          1





                                          Sorry for the error, very obviously i have my head still wrapped in python 2. @StanleyWilkins zip transforms multiple lists in a single list of tuples, which is indeed handy for some iterations. Fruit name and animal are the loop iterators, they are single values; the lists they are iterating are tableData[n].

                                          – Arthur Havlicek
                                          Dec 28 '15 at 10:14





                                          Sorry for the error, very obviously i have my head still wrapped in python 2. @StanleyWilkins zip transforms multiple lists in a single list of tuples, which is indeed handy for some iterations. Fruit name and animal are the loop iterators, they are single values; the lists they are iterating are tableData[n].

                                          – Arthur Havlicek
                                          Dec 28 '15 at 10:14











                                          2














                                          that's my method to solve this problem.



                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                          ['Alice', 'Bob', 'Carol', 'David'],
                                          ['dogs', 'cats', 'moose', 'goose']]


                                          def printTable(mylist):
                                          #getting the item who has the max length in the inner tables
                                          maxLength = 0
                                          for item in mylist:
                                          for i in item:
                                          if len(i) > maxLength:
                                          maxLength = len(i)
                                          else:
                                          maxLength = maxLength
                                          # make a seperated rjust for every item in the inner lists
                                          for item in mylist:
                                          for i in range(len(item)):
                                          item[i] = (item[i].rjust(maxLength))
                                          # convert list to dictionary data type it's more easier to deal with.
                                          myNewlist = {0: , 1: , 2: , 3: }
                                          for i in range(len(item)):
                                          for u in tableData:
                                          myNewlist[i].append(u[i])
                                          # print the out put :)
                                          for key, value in myNewlist.items():
                                          print(''.join(value))


                                          (printTable(tableData))





                                          share|improve this answer






























                                            2














                                            that's my method to solve this problem.



                                            tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                            ['Alice', 'Bob', 'Carol', 'David'],
                                            ['dogs', 'cats', 'moose', 'goose']]


                                            def printTable(mylist):
                                            #getting the item who has the max length in the inner tables
                                            maxLength = 0
                                            for item in mylist:
                                            for i in item:
                                            if len(i) > maxLength:
                                            maxLength = len(i)
                                            else:
                                            maxLength = maxLength
                                            # make a seperated rjust for every item in the inner lists
                                            for item in mylist:
                                            for i in range(len(item)):
                                            item[i] = (item[i].rjust(maxLength))
                                            # convert list to dictionary data type it's more easier to deal with.
                                            myNewlist = {0: , 1: , 2: , 3: }
                                            for i in range(len(item)):
                                            for u in tableData:
                                            myNewlist[i].append(u[i])
                                            # print the out put :)
                                            for key, value in myNewlist.items():
                                            print(''.join(value))


                                            (printTable(tableData))





                                            share|improve this answer




























                                              2












                                              2








                                              2







                                              that's my method to solve this problem.



                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                              ['Alice', 'Bob', 'Carol', 'David'],
                                              ['dogs', 'cats', 'moose', 'goose']]


                                              def printTable(mylist):
                                              #getting the item who has the max length in the inner tables
                                              maxLength = 0
                                              for item in mylist:
                                              for i in item:
                                              if len(i) > maxLength:
                                              maxLength = len(i)
                                              else:
                                              maxLength = maxLength
                                              # make a seperated rjust for every item in the inner lists
                                              for item in mylist:
                                              for i in range(len(item)):
                                              item[i] = (item[i].rjust(maxLength))
                                              # convert list to dictionary data type it's more easier to deal with.
                                              myNewlist = {0: , 1: , 2: , 3: }
                                              for i in range(len(item)):
                                              for u in tableData:
                                              myNewlist[i].append(u[i])
                                              # print the out put :)
                                              for key, value in myNewlist.items():
                                              print(''.join(value))


                                              (printTable(tableData))





                                              share|improve this answer















                                              that's my method to solve this problem.



                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                              ['Alice', 'Bob', 'Carol', 'David'],
                                              ['dogs', 'cats', 'moose', 'goose']]


                                              def printTable(mylist):
                                              #getting the item who has the max length in the inner tables
                                              maxLength = 0
                                              for item in mylist:
                                              for i in item:
                                              if len(i) > maxLength:
                                              maxLength = len(i)
                                              else:
                                              maxLength = maxLength
                                              # make a seperated rjust for every item in the inner lists
                                              for item in mylist:
                                              for i in range(len(item)):
                                              item[i] = (item[i].rjust(maxLength))
                                              # convert list to dictionary data type it's more easier to deal with.
                                              myNewlist = {0: , 1: , 2: , 3: }
                                              for i in range(len(item)):
                                              for u in tableData:
                                              myNewlist[i].append(u[i])
                                              # print the out put :)
                                              for key, value in myNewlist.items():
                                              print(''.join(value))


                                              (printTable(tableData))






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited May 22 '18 at 13:56

























                                              answered May 22 '18 at 13:51









                                              kali omarkali omar

                                              212




                                              212























                                                  2














                                                  This is how I did.



                                                  For the first part of the code I just used the hint they give to us.



                                                  In Chapter 4 / Practice Project / Character Picture Grid we've learned how to "rotate" and then print a list of lists. It was useful for the second part of my code.



                                                  #!/usr/bin/python3
                                                  # you can think of x and y as coordinates

                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                  def printTable(table):
                                                  # create a new list of 3 "0" values: one for each list in tableData
                                                  colWidths = [0] * len(table)
                                                  # search for the longest string in each list of tableData
                                                  # and put the numbers of characters in the new list
                                                  for y in range(len(table)):
                                                  for x in table[y]:
                                                  if colWidths[y] < len(x):
                                                  colWidths[y] = len(x)

                                                  # "rotate" and print the list of lists
                                                  for x in range(len(table[0])) :
                                                  for y in range(len(table)) :
                                                  print(table[y][x].rjust(colWidths[y]), end = ' ')
                                                  print()
                                                  x += 1

                                                  printTable(tableData)





                                                  share|improve this answer
























                                                  • thanks for this! my solution had me scratching my head. as far as I can tell, the 'x += 1' is unnecessary

                                                    – kfrncs
                                                    Oct 19 '18 at 16:12


















                                                  2














                                                  This is how I did.



                                                  For the first part of the code I just used the hint they give to us.



                                                  In Chapter 4 / Practice Project / Character Picture Grid we've learned how to "rotate" and then print a list of lists. It was useful for the second part of my code.



                                                  #!/usr/bin/python3
                                                  # you can think of x and y as coordinates

                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                  def printTable(table):
                                                  # create a new list of 3 "0" values: one for each list in tableData
                                                  colWidths = [0] * len(table)
                                                  # search for the longest string in each list of tableData
                                                  # and put the numbers of characters in the new list
                                                  for y in range(len(table)):
                                                  for x in table[y]:
                                                  if colWidths[y] < len(x):
                                                  colWidths[y] = len(x)

                                                  # "rotate" and print the list of lists
                                                  for x in range(len(table[0])) :
                                                  for y in range(len(table)) :
                                                  print(table[y][x].rjust(colWidths[y]), end = ' ')
                                                  print()
                                                  x += 1

                                                  printTable(tableData)





                                                  share|improve this answer
























                                                  • thanks for this! my solution had me scratching my head. as far as I can tell, the 'x += 1' is unnecessary

                                                    – kfrncs
                                                    Oct 19 '18 at 16:12
















                                                  2












                                                  2








                                                  2







                                                  This is how I did.



                                                  For the first part of the code I just used the hint they give to us.



                                                  In Chapter 4 / Practice Project / Character Picture Grid we've learned how to "rotate" and then print a list of lists. It was useful for the second part of my code.



                                                  #!/usr/bin/python3
                                                  # you can think of x and y as coordinates

                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                  def printTable(table):
                                                  # create a new list of 3 "0" values: one for each list in tableData
                                                  colWidths = [0] * len(table)
                                                  # search for the longest string in each list of tableData
                                                  # and put the numbers of characters in the new list
                                                  for y in range(len(table)):
                                                  for x in table[y]:
                                                  if colWidths[y] < len(x):
                                                  colWidths[y] = len(x)

                                                  # "rotate" and print the list of lists
                                                  for x in range(len(table[0])) :
                                                  for y in range(len(table)) :
                                                  print(table[y][x].rjust(colWidths[y]), end = ' ')
                                                  print()
                                                  x += 1

                                                  printTable(tableData)





                                                  share|improve this answer













                                                  This is how I did.



                                                  For the first part of the code I just used the hint they give to us.



                                                  In Chapter 4 / Practice Project / Character Picture Grid we've learned how to "rotate" and then print a list of lists. It was useful for the second part of my code.



                                                  #!/usr/bin/python3
                                                  # you can think of x and y as coordinates

                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                  def printTable(table):
                                                  # create a new list of 3 "0" values: one for each list in tableData
                                                  colWidths = [0] * len(table)
                                                  # search for the longest string in each list of tableData
                                                  # and put the numbers of characters in the new list
                                                  for y in range(len(table)):
                                                  for x in table[y]:
                                                  if colWidths[y] < len(x):
                                                  colWidths[y] = len(x)

                                                  # "rotate" and print the list of lists
                                                  for x in range(len(table[0])) :
                                                  for y in range(len(table)) :
                                                  print(table[y][x].rjust(colWidths[y]), end = ' ')
                                                  print()
                                                  x += 1

                                                  printTable(tableData)






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jun 12 '18 at 15:14









                                                  Jean-Charles HervéJean-Charles Hervé

                                                  212




                                                  212













                                                  • thanks for this! my solution had me scratching my head. as far as I can tell, the 'x += 1' is unnecessary

                                                    – kfrncs
                                                    Oct 19 '18 at 16:12





















                                                  • thanks for this! my solution had me scratching my head. as far as I can tell, the 'x += 1' is unnecessary

                                                    – kfrncs
                                                    Oct 19 '18 at 16:12



















                                                  thanks for this! my solution had me scratching my head. as far as I can tell, the 'x += 1' is unnecessary

                                                  – kfrncs
                                                  Oct 19 '18 at 16:12







                                                  thanks for this! my solution had me scratching my head. as far as I can tell, the 'x += 1' is unnecessary

                                                  – kfrncs
                                                  Oct 19 '18 at 16:12













                                                  1














                                                  First join elements, then find the longest one and then you can use %*s to write lines. More in comments in code.



                                                  tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                  longest = 0 # to find the longest line
                                                  lines = # to keep lines

                                                  for elements in zip(tableData[0], tableData[1], tableData[2]):

                                                  # join elements in line - like 'apples' + ' ' + 'Alice' + ' ' + 'dogs'
                                                  line = ' '.join(elements)

                                                  # add line to the list
                                                  lines.append(line)

                                                  #print(line) # you can print it to see what you get

                                                  # find the longest line
                                                  length = len(line)
                                                  if length > longest:
                                                  longest = length

                                                  #print('the longest:', longest)

                                                  longest += 1 # to get one space more at left side

                                                  # print lines using `%*s`
                                                  # if `longest` is 21 then it will works as `%21s`
                                                  for line in lines:
                                                  print('%*s' % (longest, line))





                                                  share|improve this answer






























                                                    1














                                                    First join elements, then find the longest one and then you can use %*s to write lines. More in comments in code.



                                                    tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                                    ['Alice', 'Bob', 'Carol', 'David'],
                                                    ['dogs', 'cats', 'moose', 'goose']]

                                                    longest = 0 # to find the longest line
                                                    lines = # to keep lines

                                                    for elements in zip(tableData[0], tableData[1], tableData[2]):

                                                    # join elements in line - like 'apples' + ' ' + 'Alice' + ' ' + 'dogs'
                                                    line = ' '.join(elements)

                                                    # add line to the list
                                                    lines.append(line)

                                                    #print(line) # you can print it to see what you get

                                                    # find the longest line
                                                    length = len(line)
                                                    if length > longest:
                                                    longest = length

                                                    #print('the longest:', longest)

                                                    longest += 1 # to get one space more at left side

                                                    # print lines using `%*s`
                                                    # if `longest` is 21 then it will works as `%21s`
                                                    for line in lines:
                                                    print('%*s' % (longest, line))





                                                    share|improve this answer




























                                                      1












                                                      1








                                                      1







                                                      First join elements, then find the longest one and then you can use %*s to write lines. More in comments in code.



                                                      tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                      longest = 0 # to find the longest line
                                                      lines = # to keep lines

                                                      for elements in zip(tableData[0], tableData[1], tableData[2]):

                                                      # join elements in line - like 'apples' + ' ' + 'Alice' + ' ' + 'dogs'
                                                      line = ' '.join(elements)

                                                      # add line to the list
                                                      lines.append(line)

                                                      #print(line) # you can print it to see what you get

                                                      # find the longest line
                                                      length = len(line)
                                                      if length > longest:
                                                      longest = length

                                                      #print('the longest:', longest)

                                                      longest += 1 # to get one space more at left side

                                                      # print lines using `%*s`
                                                      # if `longest` is 21 then it will works as `%21s`
                                                      for line in lines:
                                                      print('%*s' % (longest, line))





                                                      share|improve this answer















                                                      First join elements, then find the longest one and then you can use %*s to write lines. More in comments in code.



                                                      tableData=[['apples', 'oranges', 'cherries', 'banana'],
                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                      longest = 0 # to find the longest line
                                                      lines = # to keep lines

                                                      for elements in zip(tableData[0], tableData[1], tableData[2]):

                                                      # join elements in line - like 'apples' + ' ' + 'Alice' + ' ' + 'dogs'
                                                      line = ' '.join(elements)

                                                      # add line to the list
                                                      lines.append(line)

                                                      #print(line) # you can print it to see what you get

                                                      # find the longest line
                                                      length = len(line)
                                                      if length > longest:
                                                      longest = length

                                                      #print('the longest:', longest)

                                                      longest += 1 # to get one space more at left side

                                                      # print lines using `%*s`
                                                      # if `longest` is 21 then it will works as `%21s`
                                                      for line in lines:
                                                      print('%*s' % (longest, line))






                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited Dec 28 '15 at 6:39

























                                                      answered Dec 28 '15 at 6:31









                                                      furasfuras

                                                      42.6k53256




                                                      42.6k53256























                                                          1














                                                          Here is a solution. It works even if no.of inner lists changes or no.of elements in inner list changes given all inner lists have the same no.of elements.



                                                          tableData = [
                                                          ['apples', 'oranges', 'cherries', 'banana'],
                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                          ['dogs', 'cats', 'moose', 'goose']
                                                          ]

                                                          col_widths = list()
                                                          for i, record in enumerate(tableData):
                                                          col_widths.insert(i, max(len(item) for item in record))

                                                          for i in range(len(tableData[0])):
                                                          print(' '.join(record[i].rjust(col_widths[j]) for j, record in enumerate(tableData)))





                                                          share|improve this answer
























                                                          • Nice, short and clean answer!

                                                            – Frieder
                                                            Aug 31 '18 at 13:24
















                                                          1














                                                          Here is a solution. It works even if no.of inner lists changes or no.of elements in inner list changes given all inner lists have the same no.of elements.



                                                          tableData = [
                                                          ['apples', 'oranges', 'cherries', 'banana'],
                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                          ['dogs', 'cats', 'moose', 'goose']
                                                          ]

                                                          col_widths = list()
                                                          for i, record in enumerate(tableData):
                                                          col_widths.insert(i, max(len(item) for item in record))

                                                          for i in range(len(tableData[0])):
                                                          print(' '.join(record[i].rjust(col_widths[j]) for j, record in enumerate(tableData)))





                                                          share|improve this answer
























                                                          • Nice, short and clean answer!

                                                            – Frieder
                                                            Aug 31 '18 at 13:24














                                                          1












                                                          1








                                                          1







                                                          Here is a solution. It works even if no.of inner lists changes or no.of elements in inner list changes given all inner lists have the same no.of elements.



                                                          tableData = [
                                                          ['apples', 'oranges', 'cherries', 'banana'],
                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                          ['dogs', 'cats', 'moose', 'goose']
                                                          ]

                                                          col_widths = list()
                                                          for i, record in enumerate(tableData):
                                                          col_widths.insert(i, max(len(item) for item in record))

                                                          for i in range(len(tableData[0])):
                                                          print(' '.join(record[i].rjust(col_widths[j]) for j, record in enumerate(tableData)))





                                                          share|improve this answer













                                                          Here is a solution. It works even if no.of inner lists changes or no.of elements in inner list changes given all inner lists have the same no.of elements.



                                                          tableData = [
                                                          ['apples', 'oranges', 'cherries', 'banana'],
                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                          ['dogs', 'cats', 'moose', 'goose']
                                                          ]

                                                          col_widths = list()
                                                          for i, record in enumerate(tableData):
                                                          col_widths.insert(i, max(len(item) for item in record))

                                                          for i in range(len(tableData[0])):
                                                          print(' '.join(record[i].rjust(col_widths[j]) for j, record in enumerate(tableData)))






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jul 10 '16 at 13:47









                                                          pavanpavan

                                                          2,20421721




                                                          2,20421721













                                                          • Nice, short and clean answer!

                                                            – Frieder
                                                            Aug 31 '18 at 13:24



















                                                          • Nice, short and clean answer!

                                                            – Frieder
                                                            Aug 31 '18 at 13:24

















                                                          Nice, short and clean answer!

                                                          – Frieder
                                                          Aug 31 '18 at 13:24





                                                          Nice, short and clean answer!

                                                          – Frieder
                                                          Aug 31 '18 at 13:24











                                                          1














                                                          I know it has been years, but i started reading the book couple of weeks ago and this is how figured out that one :'D



                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'], 
                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                          ['dogs', 'cats', 'moose', 'goose']]
                                                          n=0
                                                          x=''
                                                          colWidths=[0]*len(tableData)

                                                          for i in range(len(tableData)):
                                                          for n in range(len(tableData[0])-1):
                                                          if colWidths[i]<len(tableData[i][n])+1:
                                                          colWidths[i]=len(tableData[i][n])+1

                                                          for n in range(len(tableData[n])):
                                                          x=''
                                                          for i in range(len(tableData)):
                                                          x+=str(tableData[i][n]).rjust(colWidths[i])

                                                          print(x)





                                                          share|improve this answer




























                                                            1














                                                            I know it has been years, but i started reading the book couple of weeks ago and this is how figured out that one :'D



                                                            tableData = [['apples', 'oranges', 'cherries', 'banana'], 
                                                            ['Alice', 'Bob', 'Carol', 'David'],
                                                            ['dogs', 'cats', 'moose', 'goose']]
                                                            n=0
                                                            x=''
                                                            colWidths=[0]*len(tableData)

                                                            for i in range(len(tableData)):
                                                            for n in range(len(tableData[0])-1):
                                                            if colWidths[i]<len(tableData[i][n])+1:
                                                            colWidths[i]=len(tableData[i][n])+1

                                                            for n in range(len(tableData[n])):
                                                            x=''
                                                            for i in range(len(tableData)):
                                                            x+=str(tableData[i][n]).rjust(colWidths[i])

                                                            print(x)





                                                            share|improve this answer


























                                                              1












                                                              1








                                                              1







                                                              I know it has been years, but i started reading the book couple of weeks ago and this is how figured out that one :'D



                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'], 
                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                              ['dogs', 'cats', 'moose', 'goose']]
                                                              n=0
                                                              x=''
                                                              colWidths=[0]*len(tableData)

                                                              for i in range(len(tableData)):
                                                              for n in range(len(tableData[0])-1):
                                                              if colWidths[i]<len(tableData[i][n])+1:
                                                              colWidths[i]=len(tableData[i][n])+1

                                                              for n in range(len(tableData[n])):
                                                              x=''
                                                              for i in range(len(tableData)):
                                                              x+=str(tableData[i][n]).rjust(colWidths[i])

                                                              print(x)





                                                              share|improve this answer













                                                              I know it has been years, but i started reading the book couple of weeks ago and this is how figured out that one :'D



                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'], 
                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                              ['dogs', 'cats', 'moose', 'goose']]
                                                              n=0
                                                              x=''
                                                              colWidths=[0]*len(tableData)

                                                              for i in range(len(tableData)):
                                                              for n in range(len(tableData[0])-1):
                                                              if colWidths[i]<len(tableData[i][n])+1:
                                                              colWidths[i]=len(tableData[i][n])+1

                                                              for n in range(len(tableData[n])):
                                                              x=''
                                                              for i in range(len(tableData)):
                                                              x+=str(tableData[i][n]).rjust(colWidths[i])

                                                              print(x)






                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Oct 26 '18 at 20:44









                                                              nemsonemso

                                                              112




                                                              112























                                                                  0














                                                                  I was having exactly the opposite problem: I had already figured out how to determine the parameter for right-justification, and how to right-justify the items. Yet I had difficulty printing several items in one single line. I tried the "end=''" but the output still looked strange. Eventually I tried to concatenate the items to be printed in one line and call the print function one more time in the loop. And it worked.



                                                                  It took me hours to do this simple exercise but it was definitely worth it!:) It feels really good looking back at how all the incremental improvements finally made the code work!



                                                                  Here is my code. Hopefully it will help.



                                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                                  def printTable(tableData):
                                                                  colWidths = [0] * len(tableData)
                                                                  for i in range(len(tableData)):
                                                                  for j in range(len(tableData[i])):
                                                                  if colWidths[i] <= len(tableData[i][j]):
                                                                  colWidths[i] = len(tableData[i][j])
                                                                  else:
                                                                  colWidths[i] = colWidths[i]

                                                                  for j in range(len(tableData[i])):
                                                                  for i in range(len(tableData)):
                                                                  print(''.join(tableData[i][j].rjust(colWidths[i] + 1)), end = '')
                                                                  #the "+ 1" is used to allow for a space in between
                                                                  print()

                                                                  printTable(tableData)


                                                                  Btw, I was surprised that



                                                                  for j in range(len(tableData[i])):
                                                                  for i in range(len(tableData)):


                                                                  actually worked.



                                                                  Shouldn't one always use i before j in this case? It seemed counterintuitive to me yet when gave it a try anyway it miraculously worked.






                                                                  share|improve this answer




























                                                                    0














                                                                    I was having exactly the opposite problem: I had already figured out how to determine the parameter for right-justification, and how to right-justify the items. Yet I had difficulty printing several items in one single line. I tried the "end=''" but the output still looked strange. Eventually I tried to concatenate the items to be printed in one line and call the print function one more time in the loop. And it worked.



                                                                    It took me hours to do this simple exercise but it was definitely worth it!:) It feels really good looking back at how all the incremental improvements finally made the code work!



                                                                    Here is my code. Hopefully it will help.



                                                                    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                    ['Alice', 'Bob', 'Carol', 'David'],
                                                                    ['dogs', 'cats', 'moose', 'goose']]

                                                                    def printTable(tableData):
                                                                    colWidths = [0] * len(tableData)
                                                                    for i in range(len(tableData)):
                                                                    for j in range(len(tableData[i])):
                                                                    if colWidths[i] <= len(tableData[i][j]):
                                                                    colWidths[i] = len(tableData[i][j])
                                                                    else:
                                                                    colWidths[i] = colWidths[i]

                                                                    for j in range(len(tableData[i])):
                                                                    for i in range(len(tableData)):
                                                                    print(''.join(tableData[i][j].rjust(colWidths[i] + 1)), end = '')
                                                                    #the "+ 1" is used to allow for a space in between
                                                                    print()

                                                                    printTable(tableData)


                                                                    Btw, I was surprised that



                                                                    for j in range(len(tableData[i])):
                                                                    for i in range(len(tableData)):


                                                                    actually worked.



                                                                    Shouldn't one always use i before j in this case? It seemed counterintuitive to me yet when gave it a try anyway it miraculously worked.






                                                                    share|improve this answer


























                                                                      0












                                                                      0








                                                                      0







                                                                      I was having exactly the opposite problem: I had already figured out how to determine the parameter for right-justification, and how to right-justify the items. Yet I had difficulty printing several items in one single line. I tried the "end=''" but the output still looked strange. Eventually I tried to concatenate the items to be printed in one line and call the print function one more time in the loop. And it worked.



                                                                      It took me hours to do this simple exercise but it was definitely worth it!:) It feels really good looking back at how all the incremental improvements finally made the code work!



                                                                      Here is my code. Hopefully it will help.



                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                      def printTable(tableData):
                                                                      colWidths = [0] * len(tableData)
                                                                      for i in range(len(tableData)):
                                                                      for j in range(len(tableData[i])):
                                                                      if colWidths[i] <= len(tableData[i][j]):
                                                                      colWidths[i] = len(tableData[i][j])
                                                                      else:
                                                                      colWidths[i] = colWidths[i]

                                                                      for j in range(len(tableData[i])):
                                                                      for i in range(len(tableData)):
                                                                      print(''.join(tableData[i][j].rjust(colWidths[i] + 1)), end = '')
                                                                      #the "+ 1" is used to allow for a space in between
                                                                      print()

                                                                      printTable(tableData)


                                                                      Btw, I was surprised that



                                                                      for j in range(len(tableData[i])):
                                                                      for i in range(len(tableData)):


                                                                      actually worked.



                                                                      Shouldn't one always use i before j in this case? It seemed counterintuitive to me yet when gave it a try anyway it miraculously worked.






                                                                      share|improve this answer













                                                                      I was having exactly the opposite problem: I had already figured out how to determine the parameter for right-justification, and how to right-justify the items. Yet I had difficulty printing several items in one single line. I tried the "end=''" but the output still looked strange. Eventually I tried to concatenate the items to be printed in one line and call the print function one more time in the loop. And it worked.



                                                                      It took me hours to do this simple exercise but it was definitely worth it!:) It feels really good looking back at how all the incremental improvements finally made the code work!



                                                                      Here is my code. Hopefully it will help.



                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                      def printTable(tableData):
                                                                      colWidths = [0] * len(tableData)
                                                                      for i in range(len(tableData)):
                                                                      for j in range(len(tableData[i])):
                                                                      if colWidths[i] <= len(tableData[i][j]):
                                                                      colWidths[i] = len(tableData[i][j])
                                                                      else:
                                                                      colWidths[i] = colWidths[i]

                                                                      for j in range(len(tableData[i])):
                                                                      for i in range(len(tableData)):
                                                                      print(''.join(tableData[i][j].rjust(colWidths[i] + 1)), end = '')
                                                                      #the "+ 1" is used to allow for a space in between
                                                                      print()

                                                                      printTable(tableData)


                                                                      Btw, I was surprised that



                                                                      for j in range(len(tableData[i])):
                                                                      for i in range(len(tableData)):


                                                                      actually worked.



                                                                      Shouldn't one always use i before j in this case? It seemed counterintuitive to me yet when gave it a try anyway it miraculously worked.







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Jan 25 '16 at 8:31









                                                                      Randy GaoRandy Gao

                                                                      1




                                                                      1























                                                                          0














                                                                          #! python3
                                                                          #table printer prints takes a list of lists of strings and displays it in a
                                                                          #well-organized table with each column right-justified.

                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                          def printTable(data):
                                                                          #in this section we are creating a list containing each column's width
                                                                          colWidths = [0] * len(data)
                                                                          for m in range(len(colWidths)):
                                                                          for n in range(len(data[0])):
                                                                          if colWidths[m] < len(data[m][n]):
                                                                          colWidths[m] = len(data[m][n])
                                                                          #optionally you can also print colWidths for a better understanding
                                                                          #print(colWidths) will output [8, 5, 5]

                                                                          #this section of the code helps arranging the list in a table format
                                                                          for u in range(len(data[0])):
                                                                          for v in range(len(data)):
                                                                          print(data[v][u].rjust(colWidths[v] + 1), end='')
                                                                          print()

                                                                          printTable(tableData)





                                                                          share|improve this answer




























                                                                            0














                                                                            #! python3
                                                                            #table printer prints takes a list of lists of strings and displays it in a
                                                                            #well-organized table with each column right-justified.

                                                                            tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                            ['Alice', 'Bob', 'Carol', 'David'],
                                                                            ['dogs', 'cats', 'moose', 'goose']]

                                                                            def printTable(data):
                                                                            #in this section we are creating a list containing each column's width
                                                                            colWidths = [0] * len(data)
                                                                            for m in range(len(colWidths)):
                                                                            for n in range(len(data[0])):
                                                                            if colWidths[m] < len(data[m][n]):
                                                                            colWidths[m] = len(data[m][n])
                                                                            #optionally you can also print colWidths for a better understanding
                                                                            #print(colWidths) will output [8, 5, 5]

                                                                            #this section of the code helps arranging the list in a table format
                                                                            for u in range(len(data[0])):
                                                                            for v in range(len(data)):
                                                                            print(data[v][u].rjust(colWidths[v] + 1), end='')
                                                                            print()

                                                                            printTable(tableData)





                                                                            share|improve this answer


























                                                                              0












                                                                              0








                                                                              0







                                                                              #! python3
                                                                              #table printer prints takes a list of lists of strings and displays it in a
                                                                              #well-organized table with each column right-justified.

                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                              ['dogs', 'cats', 'moose', 'goose']]

                                                                              def printTable(data):
                                                                              #in this section we are creating a list containing each column's width
                                                                              colWidths = [0] * len(data)
                                                                              for m in range(len(colWidths)):
                                                                              for n in range(len(data[0])):
                                                                              if colWidths[m] < len(data[m][n]):
                                                                              colWidths[m] = len(data[m][n])
                                                                              #optionally you can also print colWidths for a better understanding
                                                                              #print(colWidths) will output [8, 5, 5]

                                                                              #this section of the code helps arranging the list in a table format
                                                                              for u in range(len(data[0])):
                                                                              for v in range(len(data)):
                                                                              print(data[v][u].rjust(colWidths[v] + 1), end='')
                                                                              print()

                                                                              printTable(tableData)





                                                                              share|improve this answer













                                                                              #! python3
                                                                              #table printer prints takes a list of lists of strings and displays it in a
                                                                              #well-organized table with each column right-justified.

                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                              ['dogs', 'cats', 'moose', 'goose']]

                                                                              def printTable(data):
                                                                              #in this section we are creating a list containing each column's width
                                                                              colWidths = [0] * len(data)
                                                                              for m in range(len(colWidths)):
                                                                              for n in range(len(data[0])):
                                                                              if colWidths[m] < len(data[m][n]):
                                                                              colWidths[m] = len(data[m][n])
                                                                              #optionally you can also print colWidths for a better understanding
                                                                              #print(colWidths) will output [8, 5, 5]

                                                                              #this section of the code helps arranging the list in a table format
                                                                              for u in range(len(data[0])):
                                                                              for v in range(len(data)):
                                                                              print(data[v][u].rjust(colWidths[v] + 1), end='')
                                                                              print()

                                                                              printTable(tableData)






                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Dec 9 '16 at 2:58









                                                                              sanju stephensanju stephen

                                                                              12




                                                                              12























                                                                                  0














                                                                                  Based on the author's hint:




                                                                                  "Hint: Your code will first have to find the longest string in each of the inner lists so that the whole column can be wide enough to fit all the strings. You can store the maximum width of each column as a list of integers. The printTable() function can begin with colWidths = [0] * len(tableData), which will create a list containing the same number of 0 values as the number of inner lists in tableData. That way, colWidths[0] can store the width of the longest string in tableData[0], colWidths[1] can store the width of the longest string in tableData[1], and so on. You can then find the largest value in the colWidths list to find out what integer width to pass to the rjust() string method."




                                                                                  Here is my answer:



                                                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                                                  ['dogs', 'cats', 'moose', 'goose']]


                                                                                  def table_printer(tab_data):
                                                                                  col_widths = [0] * len(tab_data) # creates 3 lists based on the list length
                                                                                  for j in range(len(tab_data[0])): # finds a length of 4 items (aka rows)
                                                                                  for i in range(len(tab_data)): # finds a length of 3 items (aka columns)
                                                                                  col_widths[i] = len((max(tab_data[i], key=len))) # sets the column width to the maximum length of an item in the list
                                                                                  a = tab_data[i][j]
                                                                                  print(a.rjust(col_widths[i]), end=" ") # every time we print a column, we rjust it to the max width.
                                                                                  print("n")


                                                                                  table_printer(tableData)





                                                                                  share|improve this answer






























                                                                                    0














                                                                                    Based on the author's hint:




                                                                                    "Hint: Your code will first have to find the longest string in each of the inner lists so that the whole column can be wide enough to fit all the strings. You can store the maximum width of each column as a list of integers. The printTable() function can begin with colWidths = [0] * len(tableData), which will create a list containing the same number of 0 values as the number of inner lists in tableData. That way, colWidths[0] can store the width of the longest string in tableData[0], colWidths[1] can store the width of the longest string in tableData[1], and so on. You can then find the largest value in the colWidths list to find out what integer width to pass to the rjust() string method."




                                                                                    Here is my answer:



                                                                                    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                    ['Alice', 'Bob', 'Carol', 'David'],
                                                                                    ['dogs', 'cats', 'moose', 'goose']]


                                                                                    def table_printer(tab_data):
                                                                                    col_widths = [0] * len(tab_data) # creates 3 lists based on the list length
                                                                                    for j in range(len(tab_data[0])): # finds a length of 4 items (aka rows)
                                                                                    for i in range(len(tab_data)): # finds a length of 3 items (aka columns)
                                                                                    col_widths[i] = len((max(tab_data[i], key=len))) # sets the column width to the maximum length of an item in the list
                                                                                    a = tab_data[i][j]
                                                                                    print(a.rjust(col_widths[i]), end=" ") # every time we print a column, we rjust it to the max width.
                                                                                    print("n")


                                                                                    table_printer(tableData)





                                                                                    share|improve this answer




























                                                                                      0












                                                                                      0








                                                                                      0







                                                                                      Based on the author's hint:




                                                                                      "Hint: Your code will first have to find the longest string in each of the inner lists so that the whole column can be wide enough to fit all the strings. You can store the maximum width of each column as a list of integers. The printTable() function can begin with colWidths = [0] * len(tableData), which will create a list containing the same number of 0 values as the number of inner lists in tableData. That way, colWidths[0] can store the width of the longest string in tableData[0], colWidths[1] can store the width of the longest string in tableData[1], and so on. You can then find the largest value in the colWidths list to find out what integer width to pass to the rjust() string method."




                                                                                      Here is my answer:



                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                      ['dogs', 'cats', 'moose', 'goose']]


                                                                                      def table_printer(tab_data):
                                                                                      col_widths = [0] * len(tab_data) # creates 3 lists based on the list length
                                                                                      for j in range(len(tab_data[0])): # finds a length of 4 items (aka rows)
                                                                                      for i in range(len(tab_data)): # finds a length of 3 items (aka columns)
                                                                                      col_widths[i] = len((max(tab_data[i], key=len))) # sets the column width to the maximum length of an item in the list
                                                                                      a = tab_data[i][j]
                                                                                      print(a.rjust(col_widths[i]), end=" ") # every time we print a column, we rjust it to the max width.
                                                                                      print("n")


                                                                                      table_printer(tableData)





                                                                                      share|improve this answer















                                                                                      Based on the author's hint:




                                                                                      "Hint: Your code will first have to find the longest string in each of the inner lists so that the whole column can be wide enough to fit all the strings. You can store the maximum width of each column as a list of integers. The printTable() function can begin with colWidths = [0] * len(tableData), which will create a list containing the same number of 0 values as the number of inner lists in tableData. That way, colWidths[0] can store the width of the longest string in tableData[0], colWidths[1] can store the width of the longest string in tableData[1], and so on. You can then find the largest value in the colWidths list to find out what integer width to pass to the rjust() string method."




                                                                                      Here is my answer:



                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                      ['dogs', 'cats', 'moose', 'goose']]


                                                                                      def table_printer(tab_data):
                                                                                      col_widths = [0] * len(tab_data) # creates 3 lists based on the list length
                                                                                      for j in range(len(tab_data[0])): # finds a length of 4 items (aka rows)
                                                                                      for i in range(len(tab_data)): # finds a length of 3 items (aka columns)
                                                                                      col_widths[i] = len((max(tab_data[i], key=len))) # sets the column width to the maximum length of an item in the list
                                                                                      a = tab_data[i][j]
                                                                                      print(a.rjust(col_widths[i]), end=" ") # every time we print a column, we rjust it to the max width.
                                                                                      print("n")


                                                                                      table_printer(tableData)






                                                                                      share|improve this answer














                                                                                      share|improve this answer



                                                                                      share|improve this answer








                                                                                      edited Jan 6 '17 at 20:26

























                                                                                      answered Jan 6 '17 at 20:17









                                                                                      Peter PynePeter Pyne

                                                                                      12




                                                                                      12























                                                                                          0














                                                                                          So this is what I ended up with..without too much internet help. That print line sucks however. I liked some of the above but wasn't going to copycat.



                                                                                          tableData = [['apples','oranges','cherries','banana'],
                                                                                          ['Alice','Bob','Carol','David'],
                                                                                          ['dogs','cats','moose','goose']]

                                                                                          def printTable():
                                                                                          colWidths=[0]*len(tableData)
                                                                                          for i in range(len(tableData)):
                                                                                          for x in range(len(tableData[i])):
                                                                                          if colWidths[i]<len(tableData[i][x]):
                                                                                          colWidths[i]=len(tableData[i][x])
                                                                                          for x in range(len(tableData[i])):
                                                                                          print(tableData[0][x].rjust(colWidths[0]+1) + tableData[1][x].rjust(colWidths[1]+1) + tableData[2][x].rjust(colWidths[2]+1))

                                                                                          printTable()


                                                                                          The print comes out correct, but I do not like how it doesn't allow for a dynamic use. Back to the drawing board on the print line.






                                                                                          share|improve this answer






























                                                                                            0














                                                                                            So this is what I ended up with..without too much internet help. That print line sucks however. I liked some of the above but wasn't going to copycat.



                                                                                            tableData = [['apples','oranges','cherries','banana'],
                                                                                            ['Alice','Bob','Carol','David'],
                                                                                            ['dogs','cats','moose','goose']]

                                                                                            def printTable():
                                                                                            colWidths=[0]*len(tableData)
                                                                                            for i in range(len(tableData)):
                                                                                            for x in range(len(tableData[i])):
                                                                                            if colWidths[i]<len(tableData[i][x]):
                                                                                            colWidths[i]=len(tableData[i][x])
                                                                                            for x in range(len(tableData[i])):
                                                                                            print(tableData[0][x].rjust(colWidths[0]+1) + tableData[1][x].rjust(colWidths[1]+1) + tableData[2][x].rjust(colWidths[2]+1))

                                                                                            printTable()


                                                                                            The print comes out correct, but I do not like how it doesn't allow for a dynamic use. Back to the drawing board on the print line.






                                                                                            share|improve this answer




























                                                                                              0












                                                                                              0








                                                                                              0







                                                                                              So this is what I ended up with..without too much internet help. That print line sucks however. I liked some of the above but wasn't going to copycat.



                                                                                              tableData = [['apples','oranges','cherries','banana'],
                                                                                              ['Alice','Bob','Carol','David'],
                                                                                              ['dogs','cats','moose','goose']]

                                                                                              def printTable():
                                                                                              colWidths=[0]*len(tableData)
                                                                                              for i in range(len(tableData)):
                                                                                              for x in range(len(tableData[i])):
                                                                                              if colWidths[i]<len(tableData[i][x]):
                                                                                              colWidths[i]=len(tableData[i][x])
                                                                                              for x in range(len(tableData[i])):
                                                                                              print(tableData[0][x].rjust(colWidths[0]+1) + tableData[1][x].rjust(colWidths[1]+1) + tableData[2][x].rjust(colWidths[2]+1))

                                                                                              printTable()


                                                                                              The print comes out correct, but I do not like how it doesn't allow for a dynamic use. Back to the drawing board on the print line.






                                                                                              share|improve this answer















                                                                                              So this is what I ended up with..without too much internet help. That print line sucks however. I liked some of the above but wasn't going to copycat.



                                                                                              tableData = [['apples','oranges','cherries','banana'],
                                                                                              ['Alice','Bob','Carol','David'],
                                                                                              ['dogs','cats','moose','goose']]

                                                                                              def printTable():
                                                                                              colWidths=[0]*len(tableData)
                                                                                              for i in range(len(tableData)):
                                                                                              for x in range(len(tableData[i])):
                                                                                              if colWidths[i]<len(tableData[i][x]):
                                                                                              colWidths[i]=len(tableData[i][x])
                                                                                              for x in range(len(tableData[i])):
                                                                                              print(tableData[0][x].rjust(colWidths[0]+1) + tableData[1][x].rjust(colWidths[1]+1) + tableData[2][x].rjust(colWidths[2]+1))

                                                                                              printTable()


                                                                                              The print comes out correct, but I do not like how it doesn't allow for a dynamic use. Back to the drawing board on the print line.







                                                                                              share|improve this answer














                                                                                              share|improve this answer



                                                                                              share|improve this answer








                                                                                              edited Feb 7 '17 at 14:54

























                                                                                              answered Feb 6 '17 at 20:19









                                                                                              TossaireTossaire

                                                                                              11




                                                                                              11























                                                                                                  0














                                                                                                  Here's how I did it, using both the hint and only information used in the book so far.



                                                                                                  This code works no matter how many sublists are within tableData, and no matter how many items are in each sublist.



                                                                                                  I used a loop within a loop to achieve this, and print a space after each printed item. If it's the last category item, then print a new line.



                                                                                                  tableData = [['apples', 'oranges', 'cherries', 'banana','orange'],
                                                                                                  ['Alice', 'Bob', 'Carol', 'David','Phillip'],
                                                                                                  ['dogs', 'cats', 'moose', 'goose','anteater'],
                                                                                                  ['mitsubishi','honda','toyota','ford','range rover']]


                                                                                                  def printTable(table):
                                                                                                  colWidths = [0] * len(table)
                                                                                                  for i in range(len(table)):
                                                                                                  for x in table[i]:
                                                                                                  if len(x) > colWidths[i]:
                                                                                                  colWidths[i] = len(x)
                                                                                                  print(colWidths)

                                                                                                  for i in range(len(table[0])):
                                                                                                  for x in range(len(table)):
                                                                                                  print(table[x][i].rjust(colWidths[x]),end = ' ')
                                                                                                  if x == len(table)-1:
                                                                                                  print('r')



                                                                                                  printTable(tableData)


                                                                                                  '''
                                                                                                  table[0,0] + table [1,0] + table [2,0]
                                                                                                  table[1,0] + table [1,1]

                                                                                                  '''





                                                                                                  share|improve this answer






























                                                                                                    0














                                                                                                    Here's how I did it, using both the hint and only information used in the book so far.



                                                                                                    This code works no matter how many sublists are within tableData, and no matter how many items are in each sublist.



                                                                                                    I used a loop within a loop to achieve this, and print a space after each printed item. If it's the last category item, then print a new line.



                                                                                                    tableData = [['apples', 'oranges', 'cherries', 'banana','orange'],
                                                                                                    ['Alice', 'Bob', 'Carol', 'David','Phillip'],
                                                                                                    ['dogs', 'cats', 'moose', 'goose','anteater'],
                                                                                                    ['mitsubishi','honda','toyota','ford','range rover']]


                                                                                                    def printTable(table):
                                                                                                    colWidths = [0] * len(table)
                                                                                                    for i in range(len(table)):
                                                                                                    for x in table[i]:
                                                                                                    if len(x) > colWidths[i]:
                                                                                                    colWidths[i] = len(x)
                                                                                                    print(colWidths)

                                                                                                    for i in range(len(table[0])):
                                                                                                    for x in range(len(table)):
                                                                                                    print(table[x][i].rjust(colWidths[x]),end = ' ')
                                                                                                    if x == len(table)-1:
                                                                                                    print('r')



                                                                                                    printTable(tableData)


                                                                                                    '''
                                                                                                    table[0,0] + table [1,0] + table [2,0]
                                                                                                    table[1,0] + table [1,1]

                                                                                                    '''





                                                                                                    share|improve this answer




























                                                                                                      0












                                                                                                      0








                                                                                                      0







                                                                                                      Here's how I did it, using both the hint and only information used in the book so far.



                                                                                                      This code works no matter how many sublists are within tableData, and no matter how many items are in each sublist.



                                                                                                      I used a loop within a loop to achieve this, and print a space after each printed item. If it's the last category item, then print a new line.



                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana','orange'],
                                                                                                      ['Alice', 'Bob', 'Carol', 'David','Phillip'],
                                                                                                      ['dogs', 'cats', 'moose', 'goose','anteater'],
                                                                                                      ['mitsubishi','honda','toyota','ford','range rover']]


                                                                                                      def printTable(table):
                                                                                                      colWidths = [0] * len(table)
                                                                                                      for i in range(len(table)):
                                                                                                      for x in table[i]:
                                                                                                      if len(x) > colWidths[i]:
                                                                                                      colWidths[i] = len(x)
                                                                                                      print(colWidths)

                                                                                                      for i in range(len(table[0])):
                                                                                                      for x in range(len(table)):
                                                                                                      print(table[x][i].rjust(colWidths[x]),end = ' ')
                                                                                                      if x == len(table)-1:
                                                                                                      print('r')



                                                                                                      printTable(tableData)


                                                                                                      '''
                                                                                                      table[0,0] + table [1,0] + table [2,0]
                                                                                                      table[1,0] + table [1,1]

                                                                                                      '''





                                                                                                      share|improve this answer















                                                                                                      Here's how I did it, using both the hint and only information used in the book so far.



                                                                                                      This code works no matter how many sublists are within tableData, and no matter how many items are in each sublist.



                                                                                                      I used a loop within a loop to achieve this, and print a space after each printed item. If it's the last category item, then print a new line.



                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana','orange'],
                                                                                                      ['Alice', 'Bob', 'Carol', 'David','Phillip'],
                                                                                                      ['dogs', 'cats', 'moose', 'goose','anteater'],
                                                                                                      ['mitsubishi','honda','toyota','ford','range rover']]


                                                                                                      def printTable(table):
                                                                                                      colWidths = [0] * len(table)
                                                                                                      for i in range(len(table)):
                                                                                                      for x in table[i]:
                                                                                                      if len(x) > colWidths[i]:
                                                                                                      colWidths[i] = len(x)
                                                                                                      print(colWidths)

                                                                                                      for i in range(len(table[0])):
                                                                                                      for x in range(len(table)):
                                                                                                      print(table[x][i].rjust(colWidths[x]),end = ' ')
                                                                                                      if x == len(table)-1:
                                                                                                      print('r')



                                                                                                      printTable(tableData)


                                                                                                      '''
                                                                                                      table[0,0] + table [1,0] + table [2,0]
                                                                                                      table[1,0] + table [1,1]

                                                                                                      '''






                                                                                                      share|improve this answer














                                                                                                      share|improve this answer



                                                                                                      share|improve this answer








                                                                                                      edited Aug 29 '18 at 5:30









                                                                                                      Ed Cottrell

                                                                                                      37k125480




                                                                                                      37k125480










                                                                                                      answered Aug 6 '17 at 20:24









                                                                                                      gideongideon

                                                                                                      1




                                                                                                      1























                                                                                                          0














                                                                                                          So many different solutions! The book teaches us to each range(len(x)) and I've read online that this is not a good way of getting indexes. A better suggested solution is enumerate which I used in my code, you can find more information here:



                                                                                                          https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence



                                                                                                          #! python3
                                                                                                          # printTable.py - Displays a list in a well organized table

                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                          ['dogs', 'cats', 'moose', 'goose']]
                                                                                                          columnWidth = # Creates a empty list

                                                                                                          def printtable(printdata):
                                                                                                          for data in printdata:
                                                                                                          # Adds the length of the longest string to the columnWidth list
                                                                                                          columnWidth.append(len(max(data, key=len)))
                                                                                                          # For loop to decide the determine the number of columns to cycle through
                                                                                                          for x, columnData in enumerate(printdata[0]):
                                                                                                          # For loop for the number of rows to cycle through
                                                                                                          for y, rowData in enumerate(printdata):
                                                                                                          # Print each row data with the correct justification
                                                                                                          print(printdata[y][x].rjust(columnWidth[y]), end=' ')
                                                                                                          # Create a new line before reiterating
                                                                                                          print('')

                                                                                                          printtable(tableData)





                                                                                                          share|improve this answer
























                                                                                                          • there is already an answer using enumeration: stackoverflow.com/a/38292570/2648551

                                                                                                            – colidyre
                                                                                                            Aug 30 '18 at 14:08
















                                                                                                          0














                                                                                                          So many different solutions! The book teaches us to each range(len(x)) and I've read online that this is not a good way of getting indexes. A better suggested solution is enumerate which I used in my code, you can find more information here:



                                                                                                          https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence



                                                                                                          #! python3
                                                                                                          # printTable.py - Displays a list in a well organized table

                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                          ['dogs', 'cats', 'moose', 'goose']]
                                                                                                          columnWidth = # Creates a empty list

                                                                                                          def printtable(printdata):
                                                                                                          for data in printdata:
                                                                                                          # Adds the length of the longest string to the columnWidth list
                                                                                                          columnWidth.append(len(max(data, key=len)))
                                                                                                          # For loop to decide the determine the number of columns to cycle through
                                                                                                          for x, columnData in enumerate(printdata[0]):
                                                                                                          # For loop for the number of rows to cycle through
                                                                                                          for y, rowData in enumerate(printdata):
                                                                                                          # Print each row data with the correct justification
                                                                                                          print(printdata[y][x].rjust(columnWidth[y]), end=' ')
                                                                                                          # Create a new line before reiterating
                                                                                                          print('')

                                                                                                          printtable(tableData)





                                                                                                          share|improve this answer
























                                                                                                          • there is already an answer using enumeration: stackoverflow.com/a/38292570/2648551

                                                                                                            – colidyre
                                                                                                            Aug 30 '18 at 14:08














                                                                                                          0












                                                                                                          0








                                                                                                          0







                                                                                                          So many different solutions! The book teaches us to each range(len(x)) and I've read online that this is not a good way of getting indexes. A better suggested solution is enumerate which I used in my code, you can find more information here:



                                                                                                          https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence



                                                                                                          #! python3
                                                                                                          # printTable.py - Displays a list in a well organized table

                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                          ['dogs', 'cats', 'moose', 'goose']]
                                                                                                          columnWidth = # Creates a empty list

                                                                                                          def printtable(printdata):
                                                                                                          for data in printdata:
                                                                                                          # Adds the length of the longest string to the columnWidth list
                                                                                                          columnWidth.append(len(max(data, key=len)))
                                                                                                          # For loop to decide the determine the number of columns to cycle through
                                                                                                          for x, columnData in enumerate(printdata[0]):
                                                                                                          # For loop for the number of rows to cycle through
                                                                                                          for y, rowData in enumerate(printdata):
                                                                                                          # Print each row data with the correct justification
                                                                                                          print(printdata[y][x].rjust(columnWidth[y]), end=' ')
                                                                                                          # Create a new line before reiterating
                                                                                                          print('')

                                                                                                          printtable(tableData)





                                                                                                          share|improve this answer













                                                                                                          So many different solutions! The book teaches us to each range(len(x)) and I've read online that this is not a good way of getting indexes. A better suggested solution is enumerate which I used in my code, you can find more information here:



                                                                                                          https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence



                                                                                                          #! python3
                                                                                                          # printTable.py - Displays a list in a well organized table

                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                          ['dogs', 'cats', 'moose', 'goose']]
                                                                                                          columnWidth = # Creates a empty list

                                                                                                          def printtable(printdata):
                                                                                                          for data in printdata:
                                                                                                          # Adds the length of the longest string to the columnWidth list
                                                                                                          columnWidth.append(len(max(data, key=len)))
                                                                                                          # For loop to decide the determine the number of columns to cycle through
                                                                                                          for x, columnData in enumerate(printdata[0]):
                                                                                                          # For loop for the number of rows to cycle through
                                                                                                          for y, rowData in enumerate(printdata):
                                                                                                          # Print each row data with the correct justification
                                                                                                          print(printdata[y][x].rjust(columnWidth[y]), end=' ')
                                                                                                          # Create a new line before reiterating
                                                                                                          print('')

                                                                                                          printtable(tableData)






                                                                                                          share|improve this answer












                                                                                                          share|improve this answer



                                                                                                          share|improve this answer










                                                                                                          answered Aug 30 '18 at 13:49









                                                                                                          JohnJohn

                                                                                                          61




                                                                                                          61













                                                                                                          • there is already an answer using enumeration: stackoverflow.com/a/38292570/2648551

                                                                                                            – colidyre
                                                                                                            Aug 30 '18 at 14:08



















                                                                                                          • there is already an answer using enumeration: stackoverflow.com/a/38292570/2648551

                                                                                                            – colidyre
                                                                                                            Aug 30 '18 at 14:08

















                                                                                                          there is already an answer using enumeration: stackoverflow.com/a/38292570/2648551

                                                                                                          – colidyre
                                                                                                          Aug 30 '18 at 14:08





                                                                                                          there is already an answer using enumeration: stackoverflow.com/a/38292570/2648551

                                                                                                          – colidyre
                                                                                                          Aug 30 '18 at 14:08











                                                                                                          0














                                                                                                          I think this would be the answer if you follow the book. Tips and what you have learned so far included.



                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                          ['dogs', 'cats', 'moose', 'goose']]


                                                                                                          Create the function 'printTable'. First thing is getting the length of the 3 longest string in the 3 lists and putting the integer values in de list 'colWidths'



                                                                                                          def printTable(table):
                                                                                                          colWidths = [0] * len(table) # The tip from the book
                                                                                                          for i in range(len(table)):
                                                                                                          for s in range(len(table[i])):
                                                                                                          l = len(table[i][s]) # get the length of the individual strings
                                                                                                          if colWidths[i] < l: # check wich one is the longest
                                                                                                          colWidths[i] = l # save the value in the list


                                                                                                          Next part of the function is getting the right column oirder from the items in the list. I've had some troubl;e with this part, but eventually i've got it.



                                                                                                              for x in range(len(table[0])):
                                                                                                          for y in range(len(table)):
                                                                                                          if y == len(table) - 1:
                                                                                                          print(table[y][x].rjust(colWidths[y], ' '))
                                                                                                          else:
                                                                                                          print(table[y][x].rjust(colWidths[y], ' '), end=' ')


                                                                                                          execute the function:



                                                                                                          printTable(tableData)





                                                                                                          share|improve this answer




























                                                                                                            0














                                                                                                            I think this would be the answer if you follow the book. Tips and what you have learned so far included.



                                                                                                            tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                            ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                            ['dogs', 'cats', 'moose', 'goose']]


                                                                                                            Create the function 'printTable'. First thing is getting the length of the 3 longest string in the 3 lists and putting the integer values in de list 'colWidths'



                                                                                                            def printTable(table):
                                                                                                            colWidths = [0] * len(table) # The tip from the book
                                                                                                            for i in range(len(table)):
                                                                                                            for s in range(len(table[i])):
                                                                                                            l = len(table[i][s]) # get the length of the individual strings
                                                                                                            if colWidths[i] < l: # check wich one is the longest
                                                                                                            colWidths[i] = l # save the value in the list


                                                                                                            Next part of the function is getting the right column oirder from the items in the list. I've had some troubl;e with this part, but eventually i've got it.



                                                                                                                for x in range(len(table[0])):
                                                                                                            for y in range(len(table)):
                                                                                                            if y == len(table) - 1:
                                                                                                            print(table[y][x].rjust(colWidths[y], ' '))
                                                                                                            else:
                                                                                                            print(table[y][x].rjust(colWidths[y], ' '), end=' ')


                                                                                                            execute the function:



                                                                                                            printTable(tableData)





                                                                                                            share|improve this answer


























                                                                                                              0












                                                                                                              0








                                                                                                              0







                                                                                                              I think this would be the answer if you follow the book. Tips and what you have learned so far included.



                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                              ['dogs', 'cats', 'moose', 'goose']]


                                                                                                              Create the function 'printTable'. First thing is getting the length of the 3 longest string in the 3 lists and putting the integer values in de list 'colWidths'



                                                                                                              def printTable(table):
                                                                                                              colWidths = [0] * len(table) # The tip from the book
                                                                                                              for i in range(len(table)):
                                                                                                              for s in range(len(table[i])):
                                                                                                              l = len(table[i][s]) # get the length of the individual strings
                                                                                                              if colWidths[i] < l: # check wich one is the longest
                                                                                                              colWidths[i] = l # save the value in the list


                                                                                                              Next part of the function is getting the right column oirder from the items in the list. I've had some troubl;e with this part, but eventually i've got it.



                                                                                                                  for x in range(len(table[0])):
                                                                                                              for y in range(len(table)):
                                                                                                              if y == len(table) - 1:
                                                                                                              print(table[y][x].rjust(colWidths[y], ' '))
                                                                                                              else:
                                                                                                              print(table[y][x].rjust(colWidths[y], ' '), end=' ')


                                                                                                              execute the function:



                                                                                                              printTable(tableData)





                                                                                                              share|improve this answer













                                                                                                              I think this would be the answer if you follow the book. Tips and what you have learned so far included.



                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                              ['dogs', 'cats', 'moose', 'goose']]


                                                                                                              Create the function 'printTable'. First thing is getting the length of the 3 longest string in the 3 lists and putting the integer values in de list 'colWidths'



                                                                                                              def printTable(table):
                                                                                                              colWidths = [0] * len(table) # The tip from the book
                                                                                                              for i in range(len(table)):
                                                                                                              for s in range(len(table[i])):
                                                                                                              l = len(table[i][s]) # get the length of the individual strings
                                                                                                              if colWidths[i] < l: # check wich one is the longest
                                                                                                              colWidths[i] = l # save the value in the list


                                                                                                              Next part of the function is getting the right column oirder from the items in the list. I've had some troubl;e with this part, but eventually i've got it.



                                                                                                                  for x in range(len(table[0])):
                                                                                                              for y in range(len(table)):
                                                                                                              if y == len(table) - 1:
                                                                                                              print(table[y][x].rjust(colWidths[y], ' '))
                                                                                                              else:
                                                                                                              print(table[y][x].rjust(colWidths[y], ' '), end=' ')


                                                                                                              execute the function:



                                                                                                              printTable(tableData)






                                                                                                              share|improve this answer












                                                                                                              share|improve this answer



                                                                                                              share|improve this answer










                                                                                                              answered Sep 20 '18 at 14:20









                                                                                                              RiekOhRiekOh

                                                                                                              11




                                                                                                              11























                                                                                                                  -1














                                                                                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                  def printTable(data):
                                                                                                                  for j in range(len(data[0])):
                                                                                                                  for i in range(len(data)):
                                                                                                                  #This line finds the longest item for each list and its length
                                                                                                                  x = len(max(data[i], key=len))
                                                                                                                  print(data[i][j].rjust(x), end=' ')
                                                                                                                  print()

                                                                                                                  printTable(tableData)





                                                                                                                  share|improve this answer




























                                                                                                                    -1














                                                                                                                    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                    ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                    ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                    def printTable(data):
                                                                                                                    for j in range(len(data[0])):
                                                                                                                    for i in range(len(data)):
                                                                                                                    #This line finds the longest item for each list and its length
                                                                                                                    x = len(max(data[i], key=len))
                                                                                                                    print(data[i][j].rjust(x), end=' ')
                                                                                                                    print()

                                                                                                                    printTable(tableData)





                                                                                                                    share|improve this answer


























                                                                                                                      -1












                                                                                                                      -1








                                                                                                                      -1







                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                      def printTable(data):
                                                                                                                      for j in range(len(data[0])):
                                                                                                                      for i in range(len(data)):
                                                                                                                      #This line finds the longest item for each list and its length
                                                                                                                      x = len(max(data[i], key=len))
                                                                                                                      print(data[i][j].rjust(x), end=' ')
                                                                                                                      print()

                                                                                                                      printTable(tableData)





                                                                                                                      share|improve this answer













                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                      def printTable(data):
                                                                                                                      for j in range(len(data[0])):
                                                                                                                      for i in range(len(data)):
                                                                                                                      #This line finds the longest item for each list and its length
                                                                                                                      x = len(max(data[i], key=len))
                                                                                                                      print(data[i][j].rjust(x), end=' ')
                                                                                                                      print()

                                                                                                                      printTable(tableData)






                                                                                                                      share|improve this answer












                                                                                                                      share|improve this answer



                                                                                                                      share|improve this answer










                                                                                                                      answered Aug 6 '17 at 12:46









                                                                                                                      Sam MendesSam Mendes

                                                                                                                      476




                                                                                                                      476























                                                                                                                          -1














                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]



                                                                                                                          def printTable():
                                                                                                                          #List colWidth contains the longest string in each of the inner lists
                                                                                                                          colWidth=[0]*len(tableData)

                                                                                                                          n=0
                                                                                                                          #To find the longest string in each of the inner lists and store in
                                                                                                                          colWidth
                                                                                                                          for li in tableData:
                                                                                                                          num=0
                                                                                                                          for j in li:
                                                                                                                          if(num<len(j)):
                                                                                                                          num=len(j)
                                                                                                                          colWidth[n]=num
                                                                                                                          n=n+1

                                                                                                                          #To find the largest value in the colWidths list to find out what integer
                                                                                                                          width to pass to the rjust() string method.
                                                                                                                          c=0
                                                                                                                          for i in colWidth:
                                                                                                                          if(c<i):
                                                                                                                          c=i

                                                                                                                          #To print the data
                                                                                                                          for m in range(len(tableData[0])):
                                                                                                                          for n in range(len(tableData)):
                                                                                                                          print (tableData[n][m]).rjust(c),
                                                                                                                          print('')

                                                                                                                          printTable()





                                                                                                                          share|improve this answer




























                                                                                                                            -1














                                                                                                                            tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                            ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                            ['dogs', 'cats', 'moose', 'goose']]



                                                                                                                            def printTable():
                                                                                                                            #List colWidth contains the longest string in each of the inner lists
                                                                                                                            colWidth=[0]*len(tableData)

                                                                                                                            n=0
                                                                                                                            #To find the longest string in each of the inner lists and store in
                                                                                                                            colWidth
                                                                                                                            for li in tableData:
                                                                                                                            num=0
                                                                                                                            for j in li:
                                                                                                                            if(num<len(j)):
                                                                                                                            num=len(j)
                                                                                                                            colWidth[n]=num
                                                                                                                            n=n+1

                                                                                                                            #To find the largest value in the colWidths list to find out what integer
                                                                                                                            width to pass to the rjust() string method.
                                                                                                                            c=0
                                                                                                                            for i in colWidth:
                                                                                                                            if(c<i):
                                                                                                                            c=i

                                                                                                                            #To print the data
                                                                                                                            for m in range(len(tableData[0])):
                                                                                                                            for n in range(len(tableData)):
                                                                                                                            print (tableData[n][m]).rjust(c),
                                                                                                                            print('')

                                                                                                                            printTable()





                                                                                                                            share|improve this answer


























                                                                                                                              -1












                                                                                                                              -1








                                                                                                                              -1







                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]



                                                                                                                              def printTable():
                                                                                                                              #List colWidth contains the longest string in each of the inner lists
                                                                                                                              colWidth=[0]*len(tableData)

                                                                                                                              n=0
                                                                                                                              #To find the longest string in each of the inner lists and store in
                                                                                                                              colWidth
                                                                                                                              for li in tableData:
                                                                                                                              num=0
                                                                                                                              for j in li:
                                                                                                                              if(num<len(j)):
                                                                                                                              num=len(j)
                                                                                                                              colWidth[n]=num
                                                                                                                              n=n+1

                                                                                                                              #To find the largest value in the colWidths list to find out what integer
                                                                                                                              width to pass to the rjust() string method.
                                                                                                                              c=0
                                                                                                                              for i in colWidth:
                                                                                                                              if(c<i):
                                                                                                                              c=i

                                                                                                                              #To print the data
                                                                                                                              for m in range(len(tableData[0])):
                                                                                                                              for n in range(len(tableData)):
                                                                                                                              print (tableData[n][m]).rjust(c),
                                                                                                                              print('')

                                                                                                                              printTable()





                                                                                                                              share|improve this answer













                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]



                                                                                                                              def printTable():
                                                                                                                              #List colWidth contains the longest string in each of the inner lists
                                                                                                                              colWidth=[0]*len(tableData)

                                                                                                                              n=0
                                                                                                                              #To find the longest string in each of the inner lists and store in
                                                                                                                              colWidth
                                                                                                                              for li in tableData:
                                                                                                                              num=0
                                                                                                                              for j in li:
                                                                                                                              if(num<len(j)):
                                                                                                                              num=len(j)
                                                                                                                              colWidth[n]=num
                                                                                                                              n=n+1

                                                                                                                              #To find the largest value in the colWidths list to find out what integer
                                                                                                                              width to pass to the rjust() string method.
                                                                                                                              c=0
                                                                                                                              for i in colWidth:
                                                                                                                              if(c<i):
                                                                                                                              c=i

                                                                                                                              #To print the data
                                                                                                                              for m in range(len(tableData[0])):
                                                                                                                              for n in range(len(tableData)):
                                                                                                                              print (tableData[n][m]).rjust(c),
                                                                                                                              print('')

                                                                                                                              printTable()






                                                                                                                              share|improve this answer












                                                                                                                              share|improve this answer



                                                                                                                              share|improve this answer










                                                                                                                              answered Aug 13 '17 at 8:54









                                                                                                                              Ayush GuptaAyush Gupta

                                                                                                                              46




                                                                                                                              46























                                                                                                                                  -1














                                                                                                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                  def printTable(list):
                                                                                                                                  len_list =
                                                                                                                                  for i in range(len(list)):
                                                                                                                                  len_list.append(len(max(list[i], key=len)))
                                                                                                                                  for m in range(len(list[i])):
                                                                                                                                  for i in range(len(list)):
                                                                                                                                  print(list[i][m].rjust(len_list[i]+1), end = "")
                                                                                                                                  print() #to add a new line

                                                                                                                                  printTable(tableData)





                                                                                                                                  share|improve this answer




























                                                                                                                                    -1














                                                                                                                                    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                    ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                    ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                    def printTable(list):
                                                                                                                                    len_list =
                                                                                                                                    for i in range(len(list)):
                                                                                                                                    len_list.append(len(max(list[i], key=len)))
                                                                                                                                    for m in range(len(list[i])):
                                                                                                                                    for i in range(len(list)):
                                                                                                                                    print(list[i][m].rjust(len_list[i]+1), end = "")
                                                                                                                                    print() #to add a new line

                                                                                                                                    printTable(tableData)





                                                                                                                                    share|improve this answer


























                                                                                                                                      -1












                                                                                                                                      -1








                                                                                                                                      -1







                                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                      def printTable(list):
                                                                                                                                      len_list =
                                                                                                                                      for i in range(len(list)):
                                                                                                                                      len_list.append(len(max(list[i], key=len)))
                                                                                                                                      for m in range(len(list[i])):
                                                                                                                                      for i in range(len(list)):
                                                                                                                                      print(list[i][m].rjust(len_list[i]+1), end = "")
                                                                                                                                      print() #to add a new line

                                                                                                                                      printTable(tableData)





                                                                                                                                      share|improve this answer













                                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                      def printTable(list):
                                                                                                                                      len_list =
                                                                                                                                      for i in range(len(list)):
                                                                                                                                      len_list.append(len(max(list[i], key=len)))
                                                                                                                                      for m in range(len(list[i])):
                                                                                                                                      for i in range(len(list)):
                                                                                                                                      print(list[i][m].rjust(len_list[i]+1), end = "")
                                                                                                                                      print() #to add a new line

                                                                                                                                      printTable(tableData)






                                                                                                                                      share|improve this answer












                                                                                                                                      share|improve this answer



                                                                                                                                      share|improve this answer










                                                                                                                                      answered Sep 8 '17 at 13:28









                                                                                                                                      Okszana BegyOkszana Begy

                                                                                                                                      1




                                                                                                                                      1























                                                                                                                                          -1














                                                                                                                                          I think the easiest solution is to find the length of maximum size string in the whole list(inner and outter) and then set it as argument for right justification method (rjust()) then use loops for printing the list values according to the question.



                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]


                                                                                                                                          innerlen=0

                                                                                                                                          for m in tableData:
                                                                                                                                          for n in m:
                                                                                                                                          if innerlen < len(n):
                                                                                                                                          innerlen = len(n)




                                                                                                                                          for m in range(len(tableData[0])):
                                                                                                                                          for n in range(len(tableData)):
                                                                                                                                          print(tableData[n][m].rjust(innerlen),end="")

                                                                                                                                          print("")





                                                                                                                                          share|improve this answer




























                                                                                                                                            -1














                                                                                                                                            I think the easiest solution is to find the length of maximum size string in the whole list(inner and outter) and then set it as argument for right justification method (rjust()) then use loops for printing the list values according to the question.



                                                                                                                                            tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                            ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                            ['dogs', 'cats', 'moose', 'goose']]


                                                                                                                                            innerlen=0

                                                                                                                                            for m in tableData:
                                                                                                                                            for n in m:
                                                                                                                                            if innerlen < len(n):
                                                                                                                                            innerlen = len(n)




                                                                                                                                            for m in range(len(tableData[0])):
                                                                                                                                            for n in range(len(tableData)):
                                                                                                                                            print(tableData[n][m].rjust(innerlen),end="")

                                                                                                                                            print("")





                                                                                                                                            share|improve this answer


























                                                                                                                                              -1












                                                                                                                                              -1








                                                                                                                                              -1







                                                                                                                                              I think the easiest solution is to find the length of maximum size string in the whole list(inner and outter) and then set it as argument for right justification method (rjust()) then use loops for printing the list values according to the question.



                                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]


                                                                                                                                              innerlen=0

                                                                                                                                              for m in tableData:
                                                                                                                                              for n in m:
                                                                                                                                              if innerlen < len(n):
                                                                                                                                              innerlen = len(n)




                                                                                                                                              for m in range(len(tableData[0])):
                                                                                                                                              for n in range(len(tableData)):
                                                                                                                                              print(tableData[n][m].rjust(innerlen),end="")

                                                                                                                                              print("")





                                                                                                                                              share|improve this answer













                                                                                                                                              I think the easiest solution is to find the length of maximum size string in the whole list(inner and outter) and then set it as argument for right justification method (rjust()) then use loops for printing the list values according to the question.



                                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]


                                                                                                                                              innerlen=0

                                                                                                                                              for m in tableData:
                                                                                                                                              for n in m:
                                                                                                                                              if innerlen < len(n):
                                                                                                                                              innerlen = len(n)




                                                                                                                                              for m in range(len(tableData[0])):
                                                                                                                                              for n in range(len(tableData)):
                                                                                                                                              print(tableData[n][m].rjust(innerlen),end="")

                                                                                                                                              print("")






                                                                                                                                              share|improve this answer












                                                                                                                                              share|improve this answer



                                                                                                                                              share|improve this answer










                                                                                                                                              answered Sep 12 '17 at 16:32









                                                                                                                                              Raunak PatniRaunak Patni

                                                                                                                                              15




                                                                                                                                              15























                                                                                                                                                  -1














                                                                                                                                                  Here is my approach to the exercise:



                                                                                                                                                  #!/usr/bin/env python3

                                                                                                                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                  ['dogs', 'cats', 'moose','goose']]

                                                                                                                                                  def printTable():
                                                                                                                                                  colWidths = [0] * len(tableData)

                                                                                                                                                  # find longest word in each list, convert to int
                                                                                                                                                  # and add to colWidths var
                                                                                                                                                  for i in range(len(tableData)):
                                                                                                                                                  for l in tableData[i]:
                                                                                                                                                  if len(l) >= colWidths[i]:
                                                                                                                                                  colWidths[i] = len(l)
                                                                                                                                                  # print and justify using the values from colWidths + 1
                                                                                                                                                  for t in range(4):
                                                                                                                                                  print(tableData[0][t].rjust(colWidths[0]+1) +
                                                                                                                                                  tableData[1][t].rjust(colWidths[1]+1) +
                                                                                                                                                  tableData[2][t].rjust(colWidths[2]+1))

                                                                                                                                                  printTable()





                                                                                                                                                  share|improve this answer




























                                                                                                                                                    -1














                                                                                                                                                    Here is my approach to the exercise:



                                                                                                                                                    #!/usr/bin/env python3

                                                                                                                                                    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                    ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                    ['dogs', 'cats', 'moose','goose']]

                                                                                                                                                    def printTable():
                                                                                                                                                    colWidths = [0] * len(tableData)

                                                                                                                                                    # find longest word in each list, convert to int
                                                                                                                                                    # and add to colWidths var
                                                                                                                                                    for i in range(len(tableData)):
                                                                                                                                                    for l in tableData[i]:
                                                                                                                                                    if len(l) >= colWidths[i]:
                                                                                                                                                    colWidths[i] = len(l)
                                                                                                                                                    # print and justify using the values from colWidths + 1
                                                                                                                                                    for t in range(4):
                                                                                                                                                    print(tableData[0][t].rjust(colWidths[0]+1) +
                                                                                                                                                    tableData[1][t].rjust(colWidths[1]+1) +
                                                                                                                                                    tableData[2][t].rjust(colWidths[2]+1))

                                                                                                                                                    printTable()





                                                                                                                                                    share|improve this answer


























                                                                                                                                                      -1












                                                                                                                                                      -1








                                                                                                                                                      -1







                                                                                                                                                      Here is my approach to the exercise:



                                                                                                                                                      #!/usr/bin/env python3

                                                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                      ['dogs', 'cats', 'moose','goose']]

                                                                                                                                                      def printTable():
                                                                                                                                                      colWidths = [0] * len(tableData)

                                                                                                                                                      # find longest word in each list, convert to int
                                                                                                                                                      # and add to colWidths var
                                                                                                                                                      for i in range(len(tableData)):
                                                                                                                                                      for l in tableData[i]:
                                                                                                                                                      if len(l) >= colWidths[i]:
                                                                                                                                                      colWidths[i] = len(l)
                                                                                                                                                      # print and justify using the values from colWidths + 1
                                                                                                                                                      for t in range(4):
                                                                                                                                                      print(tableData[0][t].rjust(colWidths[0]+1) +
                                                                                                                                                      tableData[1][t].rjust(colWidths[1]+1) +
                                                                                                                                                      tableData[2][t].rjust(colWidths[2]+1))

                                                                                                                                                      printTable()





                                                                                                                                                      share|improve this answer













                                                                                                                                                      Here is my approach to the exercise:



                                                                                                                                                      #!/usr/bin/env python3

                                                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                      ['dogs', 'cats', 'moose','goose']]

                                                                                                                                                      def printTable():
                                                                                                                                                      colWidths = [0] * len(tableData)

                                                                                                                                                      # find longest word in each list, convert to int
                                                                                                                                                      # and add to colWidths var
                                                                                                                                                      for i in range(len(tableData)):
                                                                                                                                                      for l in tableData[i]:
                                                                                                                                                      if len(l) >= colWidths[i]:
                                                                                                                                                      colWidths[i] = len(l)
                                                                                                                                                      # print and justify using the values from colWidths + 1
                                                                                                                                                      for t in range(4):
                                                                                                                                                      print(tableData[0][t].rjust(colWidths[0]+1) +
                                                                                                                                                      tableData[1][t].rjust(colWidths[1]+1) +
                                                                                                                                                      tableData[2][t].rjust(colWidths[2]+1))

                                                                                                                                                      printTable()






                                                                                                                                                      share|improve this answer












                                                                                                                                                      share|improve this answer



                                                                                                                                                      share|improve this answer










                                                                                                                                                      answered Oct 1 '17 at 15:18









                                                                                                                                                      SirAleXboxSirAleXbox

                                                                                                                                                      316




                                                                                                                                                      316























                                                                                                                                                          -1














                                                                                                                                                          def print_table(tab):
                                                                                                                                                          for j in range(len(tab[0])):
                                                                                                                                                          for i in range(len(tab)):
                                                                                                                                                          m = max([len(s) for s in tab[i]])
                                                                                                                                                          print(tab[i][j].rjust(m), end=' ')
                                                                                                                                                          print('')


                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                          print_table(tableData)





                                                                                                                                                          share|improve this answer




























                                                                                                                                                            -1














                                                                                                                                                            def print_table(tab):
                                                                                                                                                            for j in range(len(tab[0])):
                                                                                                                                                            for i in range(len(tab)):
                                                                                                                                                            m = max([len(s) for s in tab[i]])
                                                                                                                                                            print(tab[i][j].rjust(m), end=' ')
                                                                                                                                                            print('')


                                                                                                                                                            tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                            ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                            ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                            print_table(tableData)





                                                                                                                                                            share|improve this answer


























                                                                                                                                                              -1












                                                                                                                                                              -1








                                                                                                                                                              -1







                                                                                                                                                              def print_table(tab):
                                                                                                                                                              for j in range(len(tab[0])):
                                                                                                                                                              for i in range(len(tab)):
                                                                                                                                                              m = max([len(s) for s in tab[i]])
                                                                                                                                                              print(tab[i][j].rjust(m), end=' ')
                                                                                                                                                              print('')


                                                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                              print_table(tableData)





                                                                                                                                                              share|improve this answer













                                                                                                                                                              def print_table(tab):
                                                                                                                                                              for j in range(len(tab[0])):
                                                                                                                                                              for i in range(len(tab)):
                                                                                                                                                              m = max([len(s) for s in tab[i]])
                                                                                                                                                              print(tab[i][j].rjust(m), end=' ')
                                                                                                                                                              print('')


                                                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                              print_table(tableData)






                                                                                                                                                              share|improve this answer












                                                                                                                                                              share|improve this answer



                                                                                                                                                              share|improve this answer










                                                                                                                                                              answered Feb 18 '18 at 11:05









                                                                                                                                                              Bartosz ChojnackiBartosz Chojnacki

                                                                                                                                                              1




                                                                                                                                                              1























                                                                                                                                                                  -1














                                                                                                                                                                  Maybe not the best way, but here is my solution to the task:



                                                                                                                                                                  def printtable(listlist):
                                                                                                                                                                  # variable stores the maximum length of the words in the lists
                                                                                                                                                                  lenghtCounter = 0 #8
                                                                                                                                                                  listCounter = 0 #3
                                                                                                                                                                  dict = {}

                                                                                                                                                                  for list in listlist:
                                                                                                                                                                  listCounter += 1
                                                                                                                                                                  wordcounter = 0

                                                                                                                                                                  for pos in range(len(list)):
                                                                                                                                                                  wordcounter += 1

                                                                                                                                                                  for word in list[pos:len(list):len(list)]:
                                                                                                                                                                  dict.update({list[pos]: pos})

                                                                                                                                                                  # length counter will store the longest value
                                                                                                                                                                  if len(word) > lenghtCounter:
                                                                                                                                                                  lenghtCounter = len(word)

                                                                                                                                                                  for i in range(wordcounter):
                                                                                                                                                                  line =
                                                                                                                                                                  strline = ''

                                                                                                                                                                  for k, v in dict.items():
                                                                                                                                                                  if v == i:
                                                                                                                                                                  line.append(k)
                                                                                                                                                                  strline.join(k.ljust(lenghtCounter))

                                                                                                                                                                  for el in line:
                                                                                                                                                                  strline += el.ljust(lenghtCounter + 5)
                                                                                                                                                                  print(strline)

                                                                                                                                                                  tableData = [
                                                                                                                                                                  ['apples', 'oranges', 'cherries', 'bananas'],
                                                                                                                                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                  ['dogs', 'cats', 'moose', 'goose']
                                                                                                                                                                  ]

                                                                                                                                                                  printtable(tableData)





                                                                                                                                                                  share|improve this answer






























                                                                                                                                                                    -1














                                                                                                                                                                    Maybe not the best way, but here is my solution to the task:



                                                                                                                                                                    def printtable(listlist):
                                                                                                                                                                    # variable stores the maximum length of the words in the lists
                                                                                                                                                                    lenghtCounter = 0 #8
                                                                                                                                                                    listCounter = 0 #3
                                                                                                                                                                    dict = {}

                                                                                                                                                                    for list in listlist:
                                                                                                                                                                    listCounter += 1
                                                                                                                                                                    wordcounter = 0

                                                                                                                                                                    for pos in range(len(list)):
                                                                                                                                                                    wordcounter += 1

                                                                                                                                                                    for word in list[pos:len(list):len(list)]:
                                                                                                                                                                    dict.update({list[pos]: pos})

                                                                                                                                                                    # length counter will store the longest value
                                                                                                                                                                    if len(word) > lenghtCounter:
                                                                                                                                                                    lenghtCounter = len(word)

                                                                                                                                                                    for i in range(wordcounter):
                                                                                                                                                                    line =
                                                                                                                                                                    strline = ''

                                                                                                                                                                    for k, v in dict.items():
                                                                                                                                                                    if v == i:
                                                                                                                                                                    line.append(k)
                                                                                                                                                                    strline.join(k.ljust(lenghtCounter))

                                                                                                                                                                    for el in line:
                                                                                                                                                                    strline += el.ljust(lenghtCounter + 5)
                                                                                                                                                                    print(strline)

                                                                                                                                                                    tableData = [
                                                                                                                                                                    ['apples', 'oranges', 'cherries', 'bananas'],
                                                                                                                                                                    ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                    ['dogs', 'cats', 'moose', 'goose']
                                                                                                                                                                    ]

                                                                                                                                                                    printtable(tableData)





                                                                                                                                                                    share|improve this answer




























                                                                                                                                                                      -1












                                                                                                                                                                      -1








                                                                                                                                                                      -1







                                                                                                                                                                      Maybe not the best way, but here is my solution to the task:



                                                                                                                                                                      def printtable(listlist):
                                                                                                                                                                      # variable stores the maximum length of the words in the lists
                                                                                                                                                                      lenghtCounter = 0 #8
                                                                                                                                                                      listCounter = 0 #3
                                                                                                                                                                      dict = {}

                                                                                                                                                                      for list in listlist:
                                                                                                                                                                      listCounter += 1
                                                                                                                                                                      wordcounter = 0

                                                                                                                                                                      for pos in range(len(list)):
                                                                                                                                                                      wordcounter += 1

                                                                                                                                                                      for word in list[pos:len(list):len(list)]:
                                                                                                                                                                      dict.update({list[pos]: pos})

                                                                                                                                                                      # length counter will store the longest value
                                                                                                                                                                      if len(word) > lenghtCounter:
                                                                                                                                                                      lenghtCounter = len(word)

                                                                                                                                                                      for i in range(wordcounter):
                                                                                                                                                                      line =
                                                                                                                                                                      strline = ''

                                                                                                                                                                      for k, v in dict.items():
                                                                                                                                                                      if v == i:
                                                                                                                                                                      line.append(k)
                                                                                                                                                                      strline.join(k.ljust(lenghtCounter))

                                                                                                                                                                      for el in line:
                                                                                                                                                                      strline += el.ljust(lenghtCounter + 5)
                                                                                                                                                                      print(strline)

                                                                                                                                                                      tableData = [
                                                                                                                                                                      ['apples', 'oranges', 'cherries', 'bananas'],
                                                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                      ['dogs', 'cats', 'moose', 'goose']
                                                                                                                                                                      ]

                                                                                                                                                                      printtable(tableData)





                                                                                                                                                                      share|improve this answer















                                                                                                                                                                      Maybe not the best way, but here is my solution to the task:



                                                                                                                                                                      def printtable(listlist):
                                                                                                                                                                      # variable stores the maximum length of the words in the lists
                                                                                                                                                                      lenghtCounter = 0 #8
                                                                                                                                                                      listCounter = 0 #3
                                                                                                                                                                      dict = {}

                                                                                                                                                                      for list in listlist:
                                                                                                                                                                      listCounter += 1
                                                                                                                                                                      wordcounter = 0

                                                                                                                                                                      for pos in range(len(list)):
                                                                                                                                                                      wordcounter += 1

                                                                                                                                                                      for word in list[pos:len(list):len(list)]:
                                                                                                                                                                      dict.update({list[pos]: pos})

                                                                                                                                                                      # length counter will store the longest value
                                                                                                                                                                      if len(word) > lenghtCounter:
                                                                                                                                                                      lenghtCounter = len(word)

                                                                                                                                                                      for i in range(wordcounter):
                                                                                                                                                                      line =
                                                                                                                                                                      strline = ''

                                                                                                                                                                      for k, v in dict.items():
                                                                                                                                                                      if v == i:
                                                                                                                                                                      line.append(k)
                                                                                                                                                                      strline.join(k.ljust(lenghtCounter))

                                                                                                                                                                      for el in line:
                                                                                                                                                                      strline += el.ljust(lenghtCounter + 5)
                                                                                                                                                                      print(strline)

                                                                                                                                                                      tableData = [
                                                                                                                                                                      ['apples', 'oranges', 'cherries', 'bananas'],
                                                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                      ['dogs', 'cats', 'moose', 'goose']
                                                                                                                                                                      ]

                                                                                                                                                                      printtable(tableData)






                                                                                                                                                                      share|improve this answer














                                                                                                                                                                      share|improve this answer



                                                                                                                                                                      share|improve this answer








                                                                                                                                                                      edited Feb 18 '18 at 17:35









                                                                                                                                                                      Omar Einea

                                                                                                                                                                      2,20561630




                                                                                                                                                                      2,20561630










                                                                                                                                                                      answered Feb 18 '18 at 16:08









                                                                                                                                                                      user9377387user9377387

                                                                                                                                                                      1




                                                                                                                                                                      1























                                                                                                                                                                          -1














                                                                                                                                                                          There you go



                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries','mango'],
                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                          def printTable(table):
                                                                                                                                                                          colWid=[0]*len(tableData)
                                                                                                                                                                          i=0
                                                                                                                                                                          for row in tableData:
                                                                                                                                                                          colWid[i]=len(max(row, key=len))
                                                                                                                                                                          i+=1



                                                                                                                                                                          print(colWid)

                                                                                                                                                                          wid=max(colWid)


                                                                                                                                                                          for m in range(len(tableData[0])):

                                                                                                                                                                          for n in range(len(tableData)):

                                                                                                                                                                          print (tableData[n][m].ljust(wid),end=' ')

                                                                                                                                                                          print()

                                                                                                                                                                          printTable(tableData)





                                                                                                                                                                          share|improve this answer
























                                                                                                                                                                          • Please don't just dump your code here, write a good description for your answer. Please refer to: stackoverflow.com/help/how-to-answer

                                                                                                                                                                            – sɐunıɔןɐqɐp
                                                                                                                                                                            May 6 '18 at 14:56
















                                                                                                                                                                          -1














                                                                                                                                                                          There you go



                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries','mango'],
                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                          def printTable(table):
                                                                                                                                                                          colWid=[0]*len(tableData)
                                                                                                                                                                          i=0
                                                                                                                                                                          for row in tableData:
                                                                                                                                                                          colWid[i]=len(max(row, key=len))
                                                                                                                                                                          i+=1



                                                                                                                                                                          print(colWid)

                                                                                                                                                                          wid=max(colWid)


                                                                                                                                                                          for m in range(len(tableData[0])):

                                                                                                                                                                          for n in range(len(tableData)):

                                                                                                                                                                          print (tableData[n][m].ljust(wid),end=' ')

                                                                                                                                                                          print()

                                                                                                                                                                          printTable(tableData)





                                                                                                                                                                          share|improve this answer
























                                                                                                                                                                          • Please don't just dump your code here, write a good description for your answer. Please refer to: stackoverflow.com/help/how-to-answer

                                                                                                                                                                            – sɐunıɔןɐqɐp
                                                                                                                                                                            May 6 '18 at 14:56














                                                                                                                                                                          -1












                                                                                                                                                                          -1








                                                                                                                                                                          -1







                                                                                                                                                                          There you go



                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries','mango'],
                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                          def printTable(table):
                                                                                                                                                                          colWid=[0]*len(tableData)
                                                                                                                                                                          i=0
                                                                                                                                                                          for row in tableData:
                                                                                                                                                                          colWid[i]=len(max(row, key=len))
                                                                                                                                                                          i+=1



                                                                                                                                                                          print(colWid)

                                                                                                                                                                          wid=max(colWid)


                                                                                                                                                                          for m in range(len(tableData[0])):

                                                                                                                                                                          for n in range(len(tableData)):

                                                                                                                                                                          print (tableData[n][m].ljust(wid),end=' ')

                                                                                                                                                                          print()

                                                                                                                                                                          printTable(tableData)





                                                                                                                                                                          share|improve this answer













                                                                                                                                                                          There you go



                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries','mango'],
                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                          def printTable(table):
                                                                                                                                                                          colWid=[0]*len(tableData)
                                                                                                                                                                          i=0
                                                                                                                                                                          for row in tableData:
                                                                                                                                                                          colWid[i]=len(max(row, key=len))
                                                                                                                                                                          i+=1



                                                                                                                                                                          print(colWid)

                                                                                                                                                                          wid=max(colWid)


                                                                                                                                                                          for m in range(len(tableData[0])):

                                                                                                                                                                          for n in range(len(tableData)):

                                                                                                                                                                          print (tableData[n][m].ljust(wid),end=' ')

                                                                                                                                                                          print()

                                                                                                                                                                          printTable(tableData)






                                                                                                                                                                          share|improve this answer












                                                                                                                                                                          share|improve this answer



                                                                                                                                                                          share|improve this answer










                                                                                                                                                                          answered May 6 '18 at 14:20









                                                                                                                                                                          Rohit SanjayRohit Sanjay

                                                                                                                                                                          142




                                                                                                                                                                          142













                                                                                                                                                                          • Please don't just dump your code here, write a good description for your answer. Please refer to: stackoverflow.com/help/how-to-answer

                                                                                                                                                                            – sɐunıɔןɐqɐp
                                                                                                                                                                            May 6 '18 at 14:56



















                                                                                                                                                                          • Please don't just dump your code here, write a good description for your answer. Please refer to: stackoverflow.com/help/how-to-answer

                                                                                                                                                                            – sɐunıɔןɐqɐp
                                                                                                                                                                            May 6 '18 at 14:56

















                                                                                                                                                                          Please don't just dump your code here, write a good description for your answer. Please refer to: stackoverflow.com/help/how-to-answer

                                                                                                                                                                          – sɐunıɔןɐqɐp
                                                                                                                                                                          May 6 '18 at 14:56





                                                                                                                                                                          Please don't just dump your code here, write a good description for your answer. Please refer to: stackoverflow.com/help/how-to-answer

                                                                                                                                                                          – sɐunıɔןɐqɐp
                                                                                                                                                                          May 6 '18 at 14:56











                                                                                                                                                                          -1














                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]



                                                                                                                                                                          def find_max_length(item_list):
                                                                                                                                                                          #find the length of the "item_list" parameter
                                                                                                                                                                          colWidth = [0] * len(item_list)

                                                                                                                                                                          #an empty list created to hold a single inner list from the #"item_list" parameter
                                                                                                                                                                          not_so_emptylist =

                                                                                                                                                                          i = 0
                                                                                                                                                                          maxlength = 0 #variable to hold max length of an item in the inner list

                                                                                                                                                                          for i in range(len(item_list)):
                                                                                                                                                                          not_so_emptylist = item_list[i]
                                                                                                                                                                          for item in not_so_emptylist:
                                                                                                                                                                          if len(item) > maxlength:
                                                                                                                                                                          maxlength = len(item)
                                                                                                                                                                          colWidth[i] = maxlength
                                                                                                                                                                          maxlength = 0

                                                                                                                                                                          return colWidth

                                                                                                                                                                          #an empty list to pass colwidth to
                                                                                                                                                                          width =

                                                                                                                                                                          def print_table_data(a_list):
                                                                                                                                                                          width = find_max_length(a_list)

                                                                                                                                                                          i = 0

                                                                                                                                                                          for i in range(4):
                                                                                                                                                                          print(a_list[0][i].rjust(width[0]) + ' ' + a_list[1][i].rjust(width[1]) + ' ' + a_list[2][i].rjust(width[2]))

                                                                                                                                                                          print_table_data(a_list=tableData)





                                                                                                                                                                          share|improve this answer
























                                                                                                                                                                          • Explaining your solution can be really helpful.

                                                                                                                                                                            – onetwo12
                                                                                                                                                                            May 7 '18 at 14:27
















                                                                                                                                                                          -1














                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]



                                                                                                                                                                          def find_max_length(item_list):
                                                                                                                                                                          #find the length of the "item_list" parameter
                                                                                                                                                                          colWidth = [0] * len(item_list)

                                                                                                                                                                          #an empty list created to hold a single inner list from the #"item_list" parameter
                                                                                                                                                                          not_so_emptylist =

                                                                                                                                                                          i = 0
                                                                                                                                                                          maxlength = 0 #variable to hold max length of an item in the inner list

                                                                                                                                                                          for i in range(len(item_list)):
                                                                                                                                                                          not_so_emptylist = item_list[i]
                                                                                                                                                                          for item in not_so_emptylist:
                                                                                                                                                                          if len(item) > maxlength:
                                                                                                                                                                          maxlength = len(item)
                                                                                                                                                                          colWidth[i] = maxlength
                                                                                                                                                                          maxlength = 0

                                                                                                                                                                          return colWidth

                                                                                                                                                                          #an empty list to pass colwidth to
                                                                                                                                                                          width =

                                                                                                                                                                          def print_table_data(a_list):
                                                                                                                                                                          width = find_max_length(a_list)

                                                                                                                                                                          i = 0

                                                                                                                                                                          for i in range(4):
                                                                                                                                                                          print(a_list[0][i].rjust(width[0]) + ' ' + a_list[1][i].rjust(width[1]) + ' ' + a_list[2][i].rjust(width[2]))

                                                                                                                                                                          print_table_data(a_list=tableData)





                                                                                                                                                                          share|improve this answer
























                                                                                                                                                                          • Explaining your solution can be really helpful.

                                                                                                                                                                            – onetwo12
                                                                                                                                                                            May 7 '18 at 14:27














                                                                                                                                                                          -1












                                                                                                                                                                          -1








                                                                                                                                                                          -1







                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]



                                                                                                                                                                          def find_max_length(item_list):
                                                                                                                                                                          #find the length of the "item_list" parameter
                                                                                                                                                                          colWidth = [0] * len(item_list)

                                                                                                                                                                          #an empty list created to hold a single inner list from the #"item_list" parameter
                                                                                                                                                                          not_so_emptylist =

                                                                                                                                                                          i = 0
                                                                                                                                                                          maxlength = 0 #variable to hold max length of an item in the inner list

                                                                                                                                                                          for i in range(len(item_list)):
                                                                                                                                                                          not_so_emptylist = item_list[i]
                                                                                                                                                                          for item in not_so_emptylist:
                                                                                                                                                                          if len(item) > maxlength:
                                                                                                                                                                          maxlength = len(item)
                                                                                                                                                                          colWidth[i] = maxlength
                                                                                                                                                                          maxlength = 0

                                                                                                                                                                          return colWidth

                                                                                                                                                                          #an empty list to pass colwidth to
                                                                                                                                                                          width =

                                                                                                                                                                          def print_table_data(a_list):
                                                                                                                                                                          width = find_max_length(a_list)

                                                                                                                                                                          i = 0

                                                                                                                                                                          for i in range(4):
                                                                                                                                                                          print(a_list[0][i].rjust(width[0]) + ' ' + a_list[1][i].rjust(width[1]) + ' ' + a_list[2][i].rjust(width[2]))

                                                                                                                                                                          print_table_data(a_list=tableData)





                                                                                                                                                                          share|improve this answer













                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]



                                                                                                                                                                          def find_max_length(item_list):
                                                                                                                                                                          #find the length of the "item_list" parameter
                                                                                                                                                                          colWidth = [0] * len(item_list)

                                                                                                                                                                          #an empty list created to hold a single inner list from the #"item_list" parameter
                                                                                                                                                                          not_so_emptylist =

                                                                                                                                                                          i = 0
                                                                                                                                                                          maxlength = 0 #variable to hold max length of an item in the inner list

                                                                                                                                                                          for i in range(len(item_list)):
                                                                                                                                                                          not_so_emptylist = item_list[i]
                                                                                                                                                                          for item in not_so_emptylist:
                                                                                                                                                                          if len(item) > maxlength:
                                                                                                                                                                          maxlength = len(item)
                                                                                                                                                                          colWidth[i] = maxlength
                                                                                                                                                                          maxlength = 0

                                                                                                                                                                          return colWidth

                                                                                                                                                                          #an empty list to pass colwidth to
                                                                                                                                                                          width =

                                                                                                                                                                          def print_table_data(a_list):
                                                                                                                                                                          width = find_max_length(a_list)

                                                                                                                                                                          i = 0

                                                                                                                                                                          for i in range(4):
                                                                                                                                                                          print(a_list[0][i].rjust(width[0]) + ' ' + a_list[1][i].rjust(width[1]) + ' ' + a_list[2][i].rjust(width[2]))

                                                                                                                                                                          print_table_data(a_list=tableData)






                                                                                                                                                                          share|improve this answer












                                                                                                                                                                          share|improve this answer



                                                                                                                                                                          share|improve this answer










                                                                                                                                                                          answered May 7 '18 at 14:11









                                                                                                                                                                          Abdulrasheed AgunbiadeAbdulrasheed Agunbiade

                                                                                                                                                                          1




                                                                                                                                                                          1













                                                                                                                                                                          • Explaining your solution can be really helpful.

                                                                                                                                                                            – onetwo12
                                                                                                                                                                            May 7 '18 at 14:27



















                                                                                                                                                                          • Explaining your solution can be really helpful.

                                                                                                                                                                            – onetwo12
                                                                                                                                                                            May 7 '18 at 14:27

















                                                                                                                                                                          Explaining your solution can be really helpful.

                                                                                                                                                                          – onetwo12
                                                                                                                                                                          May 7 '18 at 14:27





                                                                                                                                                                          Explaining your solution can be really helpful.

                                                                                                                                                                          – onetwo12
                                                                                                                                                                          May 7 '18 at 14:27











                                                                                                                                                                          -1














                                                                                                                                                                          Here, first of all we have to calculate the length of longest string in each of inner list, which we will store in the "colWidths" list. After that we will simply traverse through the "tableData" list. But while printing, we need to right justify each string by maximum column width (i.e. stored in colwidth) for that string so that symmetry can be maintained.Else is just printing.



                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                          def printTable(t):
                                                                                                                                                                          colWidths=[0] * len(tableData)
                                                                                                                                                                          l=
                                                                                                                                                                          for j in range(len(t)):
                                                                                                                                                                          for i in t[j]:
                                                                                                                                                                          l+=[len(i)]
                                                                                                                                                                          colWidths[j]= max(l)
                                                                                                                                                                          l=
                                                                                                                                                                          print(colWidths)

                                                                                                                                                                          for j in range(len(t[0])):
                                                                                                                                                                          for i in range(len(t)):
                                                                                                                                                                          print(t[i][j].rjust(colWidths[i]),end=' ')
                                                                                                                                                                          print(end='n')

                                                                                                                                                                          printTable(tableData)





                                                                                                                                                                          share|improve this answer






























                                                                                                                                                                            -1














                                                                                                                                                                            Here, first of all we have to calculate the length of longest string in each of inner list, which we will store in the "colWidths" list. After that we will simply traverse through the "tableData" list. But while printing, we need to right justify each string by maximum column width (i.e. stored in colwidth) for that string so that symmetry can be maintained.Else is just printing.



                                                                                                                                                                            tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                            ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                            ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                            def printTable(t):
                                                                                                                                                                            colWidths=[0] * len(tableData)
                                                                                                                                                                            l=
                                                                                                                                                                            for j in range(len(t)):
                                                                                                                                                                            for i in t[j]:
                                                                                                                                                                            l+=[len(i)]
                                                                                                                                                                            colWidths[j]= max(l)
                                                                                                                                                                            l=
                                                                                                                                                                            print(colWidths)

                                                                                                                                                                            for j in range(len(t[0])):
                                                                                                                                                                            for i in range(len(t)):
                                                                                                                                                                            print(t[i][j].rjust(colWidths[i]),end=' ')
                                                                                                                                                                            print(end='n')

                                                                                                                                                                            printTable(tableData)





                                                                                                                                                                            share|improve this answer




























                                                                                                                                                                              -1












                                                                                                                                                                              -1








                                                                                                                                                                              -1







                                                                                                                                                                              Here, first of all we have to calculate the length of longest string in each of inner list, which we will store in the "colWidths" list. After that we will simply traverse through the "tableData" list. But while printing, we need to right justify each string by maximum column width (i.e. stored in colwidth) for that string so that symmetry can be maintained.Else is just printing.



                                                                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                              def printTable(t):
                                                                                                                                                                              colWidths=[0] * len(tableData)
                                                                                                                                                                              l=
                                                                                                                                                                              for j in range(len(t)):
                                                                                                                                                                              for i in t[j]:
                                                                                                                                                                              l+=[len(i)]
                                                                                                                                                                              colWidths[j]= max(l)
                                                                                                                                                                              l=
                                                                                                                                                                              print(colWidths)

                                                                                                                                                                              for j in range(len(t[0])):
                                                                                                                                                                              for i in range(len(t)):
                                                                                                                                                                              print(t[i][j].rjust(colWidths[i]),end=' ')
                                                                                                                                                                              print(end='n')

                                                                                                                                                                              printTable(tableData)





                                                                                                                                                                              share|improve this answer















                                                                                                                                                                              Here, first of all we have to calculate the length of longest string in each of inner list, which we will store in the "colWidths" list. After that we will simply traverse through the "tableData" list. But while printing, we need to right justify each string by maximum column width (i.e. stored in colwidth) for that string so that symmetry can be maintained.Else is just printing.



                                                                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                              def printTable(t):
                                                                                                                                                                              colWidths=[0] * len(tableData)
                                                                                                                                                                              l=
                                                                                                                                                                              for j in range(len(t)):
                                                                                                                                                                              for i in t[j]:
                                                                                                                                                                              l+=[len(i)]
                                                                                                                                                                              colWidths[j]= max(l)
                                                                                                                                                                              l=
                                                                                                                                                                              print(colWidths)

                                                                                                                                                                              for j in range(len(t[0])):
                                                                                                                                                                              for i in range(len(t)):
                                                                                                                                                                              print(t[i][j].rjust(colWidths[i]),end=' ')
                                                                                                                                                                              print(end='n')

                                                                                                                                                                              printTable(tableData)






                                                                                                                                                                              share|improve this answer














                                                                                                                                                                              share|improve this answer



                                                                                                                                                                              share|improve this answer








                                                                                                                                                                              edited May 21 '18 at 9:29

























                                                                                                                                                                              answered May 21 '18 at 6:52









                                                                                                                                                                              Arvind ThakurArvind Thakur

                                                                                                                                                                              13




                                                                                                                                                                              13























                                                                                                                                                                                  -1














                                                                                                                                                                                  def table_print(tabledata):
                                                                                                                                                                                  column=[0]*len(tabledata)
                                                                                                                                                                                  for k in range(len(tabledata)):
                                                                                                                                                                                  column[k]=len(max(tabledata[k],key=len))

                                                                                                                                                                                  for i in range(len(tabledata[0])):
                                                                                                                                                                                  for j in range(len(tabledata)):
                                                                                                                                                                                  print(tabledata[j][i].rjust(column[j]+1),end="")
                                                                                                                                                                                  print()
                                                                                                                                                                                  return
                                                                                                                                                                                  table_Data = [['app', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                  ['Ale', 'Bob', 'Crol', 'Dad'],
                                                                                                                                                                                  ['dogs', 'cats', 'moose', 'ge']]
                                                                                                                                                                                  table_print(table_Data)





                                                                                                                                                                                  share|improve this answer
























                                                                                                                                                                                  • Welcome to SO. I am sure you can add some descriptions and gotchas in the code, to make a better answer.

                                                                                                                                                                                    – Nilay Vishwakarma
                                                                                                                                                                                    Jun 11 '18 at 19:36
















                                                                                                                                                                                  -1














                                                                                                                                                                                  def table_print(tabledata):
                                                                                                                                                                                  column=[0]*len(tabledata)
                                                                                                                                                                                  for k in range(len(tabledata)):
                                                                                                                                                                                  column[k]=len(max(tabledata[k],key=len))

                                                                                                                                                                                  for i in range(len(tabledata[0])):
                                                                                                                                                                                  for j in range(len(tabledata)):
                                                                                                                                                                                  print(tabledata[j][i].rjust(column[j]+1),end="")
                                                                                                                                                                                  print()
                                                                                                                                                                                  return
                                                                                                                                                                                  table_Data = [['app', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                  ['Ale', 'Bob', 'Crol', 'Dad'],
                                                                                                                                                                                  ['dogs', 'cats', 'moose', 'ge']]
                                                                                                                                                                                  table_print(table_Data)





                                                                                                                                                                                  share|improve this answer
























                                                                                                                                                                                  • Welcome to SO. I am sure you can add some descriptions and gotchas in the code, to make a better answer.

                                                                                                                                                                                    – Nilay Vishwakarma
                                                                                                                                                                                    Jun 11 '18 at 19:36














                                                                                                                                                                                  -1












                                                                                                                                                                                  -1








                                                                                                                                                                                  -1







                                                                                                                                                                                  def table_print(tabledata):
                                                                                                                                                                                  column=[0]*len(tabledata)
                                                                                                                                                                                  for k in range(len(tabledata)):
                                                                                                                                                                                  column[k]=len(max(tabledata[k],key=len))

                                                                                                                                                                                  for i in range(len(tabledata[0])):
                                                                                                                                                                                  for j in range(len(tabledata)):
                                                                                                                                                                                  print(tabledata[j][i].rjust(column[j]+1),end="")
                                                                                                                                                                                  print()
                                                                                                                                                                                  return
                                                                                                                                                                                  table_Data = [['app', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                  ['Ale', 'Bob', 'Crol', 'Dad'],
                                                                                                                                                                                  ['dogs', 'cats', 'moose', 'ge']]
                                                                                                                                                                                  table_print(table_Data)





                                                                                                                                                                                  share|improve this answer













                                                                                                                                                                                  def table_print(tabledata):
                                                                                                                                                                                  column=[0]*len(tabledata)
                                                                                                                                                                                  for k in range(len(tabledata)):
                                                                                                                                                                                  column[k]=len(max(tabledata[k],key=len))

                                                                                                                                                                                  for i in range(len(tabledata[0])):
                                                                                                                                                                                  for j in range(len(tabledata)):
                                                                                                                                                                                  print(tabledata[j][i].rjust(column[j]+1),end="")
                                                                                                                                                                                  print()
                                                                                                                                                                                  return
                                                                                                                                                                                  table_Data = [['app', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                  ['Ale', 'Bob', 'Crol', 'Dad'],
                                                                                                                                                                                  ['dogs', 'cats', 'moose', 'ge']]
                                                                                                                                                                                  table_print(table_Data)






                                                                                                                                                                                  share|improve this answer












                                                                                                                                                                                  share|improve this answer



                                                                                                                                                                                  share|improve this answer










                                                                                                                                                                                  answered Jun 11 '18 at 19:04









                                                                                                                                                                                  B B BharadwajB B Bharadwaj

                                                                                                                                                                                  1




                                                                                                                                                                                  1













                                                                                                                                                                                  • Welcome to SO. I am sure you can add some descriptions and gotchas in the code, to make a better answer.

                                                                                                                                                                                    – Nilay Vishwakarma
                                                                                                                                                                                    Jun 11 '18 at 19:36



















                                                                                                                                                                                  • Welcome to SO. I am sure you can add some descriptions and gotchas in the code, to make a better answer.

                                                                                                                                                                                    – Nilay Vishwakarma
                                                                                                                                                                                    Jun 11 '18 at 19:36

















                                                                                                                                                                                  Welcome to SO. I am sure you can add some descriptions and gotchas in the code, to make a better answer.

                                                                                                                                                                                  – Nilay Vishwakarma
                                                                                                                                                                                  Jun 11 '18 at 19:36





                                                                                                                                                                                  Welcome to SO. I am sure you can add some descriptions and gotchas in the code, to make a better answer.

                                                                                                                                                                                  – Nilay Vishwakarma
                                                                                                                                                                                  Jun 11 '18 at 19:36











                                                                                                                                                                                  -1














                                                                                                                                                                                  It's fun to see how everyone does it differently but still gets the same result. I did it like this:



                                                                                                                                                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                  def printTable(table):
                                                                                                                                                                                  table_len =
                                                                                                                                                                                  max_of_table =
                                                                                                                                                                                  next_item = ''
                                                                                                                                                                                  for i in range(len(table)):
                                                                                                                                                                                  temp_len =
                                                                                                                                                                                  for k in range(len(table[i])):
                                                                                                                                                                                  temp_len.append(len(table[i][k]))
                                                                                                                                                                                  table_len.append(temp_len)
                                                                                                                                                                                  for b in table_len:
                                                                                                                                                                                  max_of_table.append(max(b))
                                                                                                                                                                                  for a in range(len(table[0])):
                                                                                                                                                                                  for s in range(len(table)):
                                                                                                                                                                                  next_item = str(table[s][a])
                                                                                                                                                                                  next_item = next_item.rjust(max_of_table[s])
                                                                                                                                                                                  print(next_item, end=' ')
                                                                                                                                                                                  print('')

                                                                                                                                                                                  printTable(tableData)





                                                                                                                                                                                  share|improve this answer




























                                                                                                                                                                                    -1














                                                                                                                                                                                    It's fun to see how everyone does it differently but still gets the same result. I did it like this:



                                                                                                                                                                                    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                    ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                    ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                    def printTable(table):
                                                                                                                                                                                    table_len =
                                                                                                                                                                                    max_of_table =
                                                                                                                                                                                    next_item = ''
                                                                                                                                                                                    for i in range(len(table)):
                                                                                                                                                                                    temp_len =
                                                                                                                                                                                    for k in range(len(table[i])):
                                                                                                                                                                                    temp_len.append(len(table[i][k]))
                                                                                                                                                                                    table_len.append(temp_len)
                                                                                                                                                                                    for b in table_len:
                                                                                                                                                                                    max_of_table.append(max(b))
                                                                                                                                                                                    for a in range(len(table[0])):
                                                                                                                                                                                    for s in range(len(table)):
                                                                                                                                                                                    next_item = str(table[s][a])
                                                                                                                                                                                    next_item = next_item.rjust(max_of_table[s])
                                                                                                                                                                                    print(next_item, end=' ')
                                                                                                                                                                                    print('')

                                                                                                                                                                                    printTable(tableData)





                                                                                                                                                                                    share|improve this answer


























                                                                                                                                                                                      -1












                                                                                                                                                                                      -1








                                                                                                                                                                                      -1







                                                                                                                                                                                      It's fun to see how everyone does it differently but still gets the same result. I did it like this:



                                                                                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                      def printTable(table):
                                                                                                                                                                                      table_len =
                                                                                                                                                                                      max_of_table =
                                                                                                                                                                                      next_item = ''
                                                                                                                                                                                      for i in range(len(table)):
                                                                                                                                                                                      temp_len =
                                                                                                                                                                                      for k in range(len(table[i])):
                                                                                                                                                                                      temp_len.append(len(table[i][k]))
                                                                                                                                                                                      table_len.append(temp_len)
                                                                                                                                                                                      for b in table_len:
                                                                                                                                                                                      max_of_table.append(max(b))
                                                                                                                                                                                      for a in range(len(table[0])):
                                                                                                                                                                                      for s in range(len(table)):
                                                                                                                                                                                      next_item = str(table[s][a])
                                                                                                                                                                                      next_item = next_item.rjust(max_of_table[s])
                                                                                                                                                                                      print(next_item, end=' ')
                                                                                                                                                                                      print('')

                                                                                                                                                                                      printTable(tableData)





                                                                                                                                                                                      share|improve this answer













                                                                                                                                                                                      It's fun to see how everyone does it differently but still gets the same result. I did it like this:



                                                                                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                      def printTable(table):
                                                                                                                                                                                      table_len =
                                                                                                                                                                                      max_of_table =
                                                                                                                                                                                      next_item = ''
                                                                                                                                                                                      for i in range(len(table)):
                                                                                                                                                                                      temp_len =
                                                                                                                                                                                      for k in range(len(table[i])):
                                                                                                                                                                                      temp_len.append(len(table[i][k]))
                                                                                                                                                                                      table_len.append(temp_len)
                                                                                                                                                                                      for b in table_len:
                                                                                                                                                                                      max_of_table.append(max(b))
                                                                                                                                                                                      for a in range(len(table[0])):
                                                                                                                                                                                      for s in range(len(table)):
                                                                                                                                                                                      next_item = str(table[s][a])
                                                                                                                                                                                      next_item = next_item.rjust(max_of_table[s])
                                                                                                                                                                                      print(next_item, end=' ')
                                                                                                                                                                                      print('')

                                                                                                                                                                                      printTable(tableData)






                                                                                                                                                                                      share|improve this answer












                                                                                                                                                                                      share|improve this answer



                                                                                                                                                                                      share|improve this answer










                                                                                                                                                                                      answered Aug 31 '18 at 13:16









                                                                                                                                                                                      DjaroDjaro

                                                                                                                                                                                      95




                                                                                                                                                                                      95























                                                                                                                                                                                          -1














                                                                                                                                                                                          My solution:



                                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]


                                                                                                                                                                                          def printTable(table):
                                                                                                                                                                                          liste = 0
                                                                                                                                                                                          colWidths = [0] * len(tableData)
                                                                                                                                                                                          for lister in table:
                                                                                                                                                                                          liste = liste +1
                                                                                                                                                                                          longest =0
                                                                                                                                                                                          for strenge in lister:
                                                                                                                                                                                          if len(strenge) > longest:
                                                                                                                                                                                          longest = len(strenge)
                                                                                                                                                                                          colWidths.insert((liste-1),longest)

                                                                                                                                                                                          for i in range(len(lister)):
                                                                                                                                                                                          print()
                                                                                                                                                                                          for lister in table:
                                                                                                                                                                                          print (lister[i].rjust(colWidths[0]),end='')

                                                                                                                                                                                          printTable(tableData)





                                                                                                                                                                                          share|improve this answer




























                                                                                                                                                                                            -1














                                                                                                                                                                                            My solution:



                                                                                                                                                                                            tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                            ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                            ['dogs', 'cats', 'moose', 'goose']]


                                                                                                                                                                                            def printTable(table):
                                                                                                                                                                                            liste = 0
                                                                                                                                                                                            colWidths = [0] * len(tableData)
                                                                                                                                                                                            for lister in table:
                                                                                                                                                                                            liste = liste +1
                                                                                                                                                                                            longest =0
                                                                                                                                                                                            for strenge in lister:
                                                                                                                                                                                            if len(strenge) > longest:
                                                                                                                                                                                            longest = len(strenge)
                                                                                                                                                                                            colWidths.insert((liste-1),longest)

                                                                                                                                                                                            for i in range(len(lister)):
                                                                                                                                                                                            print()
                                                                                                                                                                                            for lister in table:
                                                                                                                                                                                            print (lister[i].rjust(colWidths[0]),end='')

                                                                                                                                                                                            printTable(tableData)





                                                                                                                                                                                            share|improve this answer


























                                                                                                                                                                                              -1












                                                                                                                                                                                              -1








                                                                                                                                                                                              -1







                                                                                                                                                                                              My solution:



                                                                                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]


                                                                                                                                                                                              def printTable(table):
                                                                                                                                                                                              liste = 0
                                                                                                                                                                                              colWidths = [0] * len(tableData)
                                                                                                                                                                                              for lister in table:
                                                                                                                                                                                              liste = liste +1
                                                                                                                                                                                              longest =0
                                                                                                                                                                                              for strenge in lister:
                                                                                                                                                                                              if len(strenge) > longest:
                                                                                                                                                                                              longest = len(strenge)
                                                                                                                                                                                              colWidths.insert((liste-1),longest)

                                                                                                                                                                                              for i in range(len(lister)):
                                                                                                                                                                                              print()
                                                                                                                                                                                              for lister in table:
                                                                                                                                                                                              print (lister[i].rjust(colWidths[0]),end='')

                                                                                                                                                                                              printTable(tableData)





                                                                                                                                                                                              share|improve this answer













                                                                                                                                                                                              My solution:



                                                                                                                                                                                              tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                              ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                              ['dogs', 'cats', 'moose', 'goose']]


                                                                                                                                                                                              def printTable(table):
                                                                                                                                                                                              liste = 0
                                                                                                                                                                                              colWidths = [0] * len(tableData)
                                                                                                                                                                                              for lister in table:
                                                                                                                                                                                              liste = liste +1
                                                                                                                                                                                              longest =0
                                                                                                                                                                                              for strenge in lister:
                                                                                                                                                                                              if len(strenge) > longest:
                                                                                                                                                                                              longest = len(strenge)
                                                                                                                                                                                              colWidths.insert((liste-1),longest)

                                                                                                                                                                                              for i in range(len(lister)):
                                                                                                                                                                                              print()
                                                                                                                                                                                              for lister in table:
                                                                                                                                                                                              print (lister[i].rjust(colWidths[0]),end='')

                                                                                                                                                                                              printTable(tableData)






                                                                                                                                                                                              share|improve this answer












                                                                                                                                                                                              share|improve this answer



                                                                                                                                                                                              share|improve this answer










                                                                                                                                                                                              answered Sep 26 '18 at 19:53









                                                                                                                                                                                              JacobSivJacobSiv

                                                                                                                                                                                              11




                                                                                                                                                                                              11























                                                                                                                                                                                                  -1














                                                                                                                                                                                                  This is the simplest way I found to satisfy the goal. Here is my code:



                                                                                                                                                                                                  tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                  ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                  ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                  def printTable(myTable):

                                                                                                                                                                                                  #each column is a different width, so colWidth is a list which contains the width of each column
                                                                                                                                                                                                  #this is also suggested in the problem itself
                                                                                                                                                                                                  colWidth=[0]*len(myTable)
                                                                                                                                                                                                  for x in range(len(myTable)):
                                                                                                                                                                                                  for item in myTable[x]:
                                                                                                                                                                                                  if len(item)>colWidth[x]:
                                                                                                                                                                                                  colWidth[x]=len(item)

                                                                                                                                                                                                  #printing the table is similar to the exercise at the end of Chapter 4
                                                                                                                                                                                                  for a in range (len(myTable[0])):
                                                                                                                                                                                                  for b in range (len(myTable)):
                                                                                                                                                                                                  print (str(myTable[b][a]).rjust(colWidth[b]), end = ' ')
                                                                                                                                                                                                  print('n')

                                                                                                                                                                                                  printTable(tableData)





                                                                                                                                                                                                  share|improve this answer






























                                                                                                                                                                                                    -1














                                                                                                                                                                                                    This is the simplest way I found to satisfy the goal. Here is my code:



                                                                                                                                                                                                    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                    ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                    ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                    def printTable(myTable):

                                                                                                                                                                                                    #each column is a different width, so colWidth is a list which contains the width of each column
                                                                                                                                                                                                    #this is also suggested in the problem itself
                                                                                                                                                                                                    colWidth=[0]*len(myTable)
                                                                                                                                                                                                    for x in range(len(myTable)):
                                                                                                                                                                                                    for item in myTable[x]:
                                                                                                                                                                                                    if len(item)>colWidth[x]:
                                                                                                                                                                                                    colWidth[x]=len(item)

                                                                                                                                                                                                    #printing the table is similar to the exercise at the end of Chapter 4
                                                                                                                                                                                                    for a in range (len(myTable[0])):
                                                                                                                                                                                                    for b in range (len(myTable)):
                                                                                                                                                                                                    print (str(myTable[b][a]).rjust(colWidth[b]), end = ' ')
                                                                                                                                                                                                    print('n')

                                                                                                                                                                                                    printTable(tableData)





                                                                                                                                                                                                    share|improve this answer




























                                                                                                                                                                                                      -1












                                                                                                                                                                                                      -1








                                                                                                                                                                                                      -1







                                                                                                                                                                                                      This is the simplest way I found to satisfy the goal. Here is my code:



                                                                                                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                      def printTable(myTable):

                                                                                                                                                                                                      #each column is a different width, so colWidth is a list which contains the width of each column
                                                                                                                                                                                                      #this is also suggested in the problem itself
                                                                                                                                                                                                      colWidth=[0]*len(myTable)
                                                                                                                                                                                                      for x in range(len(myTable)):
                                                                                                                                                                                                      for item in myTable[x]:
                                                                                                                                                                                                      if len(item)>colWidth[x]:
                                                                                                                                                                                                      colWidth[x]=len(item)

                                                                                                                                                                                                      #printing the table is similar to the exercise at the end of Chapter 4
                                                                                                                                                                                                      for a in range (len(myTable[0])):
                                                                                                                                                                                                      for b in range (len(myTable)):
                                                                                                                                                                                                      print (str(myTable[b][a]).rjust(colWidth[b]), end = ' ')
                                                                                                                                                                                                      print('n')

                                                                                                                                                                                                      printTable(tableData)





                                                                                                                                                                                                      share|improve this answer















                                                                                                                                                                                                      This is the simplest way I found to satisfy the goal. Here is my code:



                                                                                                                                                                                                      tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                      ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                      ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                      def printTable(myTable):

                                                                                                                                                                                                      #each column is a different width, so colWidth is a list which contains the width of each column
                                                                                                                                                                                                      #this is also suggested in the problem itself
                                                                                                                                                                                                      colWidth=[0]*len(myTable)
                                                                                                                                                                                                      for x in range(len(myTable)):
                                                                                                                                                                                                      for item in myTable[x]:
                                                                                                                                                                                                      if len(item)>colWidth[x]:
                                                                                                                                                                                                      colWidth[x]=len(item)

                                                                                                                                                                                                      #printing the table is similar to the exercise at the end of Chapter 4
                                                                                                                                                                                                      for a in range (len(myTable[0])):
                                                                                                                                                                                                      for b in range (len(myTable)):
                                                                                                                                                                                                      print (str(myTable[b][a]).rjust(colWidth[b]), end = ' ')
                                                                                                                                                                                                      print('n')

                                                                                                                                                                                                      printTable(tableData)






                                                                                                                                                                                                      share|improve this answer














                                                                                                                                                                                                      share|improve this answer



                                                                                                                                                                                                      share|improve this answer








                                                                                                                                                                                                      edited Oct 30 '18 at 2:41

























                                                                                                                                                                                                      answered Oct 28 '18 at 23:54









                                                                                                                                                                                                      WaldoBGWaldoBG

                                                                                                                                                                                                      11




                                                                                                                                                                                                      11























                                                                                                                                                                                                          -1














                                                                                                                                                                                                          # table printer
                                                                                                                                                                                                          def printtable(tabledata,columnwidth):
                                                                                                                                                                                                          for y in range(4):
                                                                                                                                                                                                          print()
                                                                                                                                                                                                          for x in range(len(tabledata)):
                                                                                                                                                                                                          print(tabledata[x][y].rjust(columnwidth),end='')
                                                                                                                                                                                                          tabledata=[['apples','oranges','cherries','banana'],.
                                                                                                                                                                                                          ['Alice','Bob','Carol','David'],
                                                                                                                                                                                                          ['dogs','cats','moose','goose']]
                                                                                                                                                                                                          n=len(tabledata[0][0]
                                                                                                                                                                                                          for y in range(4):
                                                                                                                                                                                                          for x in range(len(tabledata)):
                                                                                                                                                                                                          if len(tabledata[0][0])>=len(tabledata[x][y]):
                                                                                                                                                                                                          False
                                                                                                                                                                                                          else:
                                                                                                                                                                                                          n=len(tabledata[x][y])
                                                                                                                                                                                                          printtable(tabledata,n)





                                                                                                                                                                                                          share|improve this answer


























                                                                                                                                                                                                          • Welcome to stack overflow. Please also add a description on what the code does instead of just providing the code.

                                                                                                                                                                                                            – Sashi
                                                                                                                                                                                                            Jan 1 at 6:52
















                                                                                                                                                                                                          -1














                                                                                                                                                                                                          # table printer
                                                                                                                                                                                                          def printtable(tabledata,columnwidth):
                                                                                                                                                                                                          for y in range(4):
                                                                                                                                                                                                          print()
                                                                                                                                                                                                          for x in range(len(tabledata)):
                                                                                                                                                                                                          print(tabledata[x][y].rjust(columnwidth),end='')
                                                                                                                                                                                                          tabledata=[['apples','oranges','cherries','banana'],.
                                                                                                                                                                                                          ['Alice','Bob','Carol','David'],
                                                                                                                                                                                                          ['dogs','cats','moose','goose']]
                                                                                                                                                                                                          n=len(tabledata[0][0]
                                                                                                                                                                                                          for y in range(4):
                                                                                                                                                                                                          for x in range(len(tabledata)):
                                                                                                                                                                                                          if len(tabledata[0][0])>=len(tabledata[x][y]):
                                                                                                                                                                                                          False
                                                                                                                                                                                                          else:
                                                                                                                                                                                                          n=len(tabledata[x][y])
                                                                                                                                                                                                          printtable(tabledata,n)





                                                                                                                                                                                                          share|improve this answer


























                                                                                                                                                                                                          • Welcome to stack overflow. Please also add a description on what the code does instead of just providing the code.

                                                                                                                                                                                                            – Sashi
                                                                                                                                                                                                            Jan 1 at 6:52














                                                                                                                                                                                                          -1












                                                                                                                                                                                                          -1








                                                                                                                                                                                                          -1







                                                                                                                                                                                                          # table printer
                                                                                                                                                                                                          def printtable(tabledata,columnwidth):
                                                                                                                                                                                                          for y in range(4):
                                                                                                                                                                                                          print()
                                                                                                                                                                                                          for x in range(len(tabledata)):
                                                                                                                                                                                                          print(tabledata[x][y].rjust(columnwidth),end='')
                                                                                                                                                                                                          tabledata=[['apples','oranges','cherries','banana'],.
                                                                                                                                                                                                          ['Alice','Bob','Carol','David'],
                                                                                                                                                                                                          ['dogs','cats','moose','goose']]
                                                                                                                                                                                                          n=len(tabledata[0][0]
                                                                                                                                                                                                          for y in range(4):
                                                                                                                                                                                                          for x in range(len(tabledata)):
                                                                                                                                                                                                          if len(tabledata[0][0])>=len(tabledata[x][y]):
                                                                                                                                                                                                          False
                                                                                                                                                                                                          else:
                                                                                                                                                                                                          n=len(tabledata[x][y])
                                                                                                                                                                                                          printtable(tabledata,n)





                                                                                                                                                                                                          share|improve this answer















                                                                                                                                                                                                          # table printer
                                                                                                                                                                                                          def printtable(tabledata,columnwidth):
                                                                                                                                                                                                          for y in range(4):
                                                                                                                                                                                                          print()
                                                                                                                                                                                                          for x in range(len(tabledata)):
                                                                                                                                                                                                          print(tabledata[x][y].rjust(columnwidth),end='')
                                                                                                                                                                                                          tabledata=[['apples','oranges','cherries','banana'],.
                                                                                                                                                                                                          ['Alice','Bob','Carol','David'],
                                                                                                                                                                                                          ['dogs','cats','moose','goose']]
                                                                                                                                                                                                          n=len(tabledata[0][0]
                                                                                                                                                                                                          for y in range(4):
                                                                                                                                                                                                          for x in range(len(tabledata)):
                                                                                                                                                                                                          if len(tabledata[0][0])>=len(tabledata[x][y]):
                                                                                                                                                                                                          False
                                                                                                                                                                                                          else:
                                                                                                                                                                                                          n=len(tabledata[x][y])
                                                                                                                                                                                                          printtable(tabledata,n)






                                                                                                                                                                                                          share|improve this answer














                                                                                                                                                                                                          share|improve this answer



                                                                                                                                                                                                          share|improve this answer








                                                                                                                                                                                                          edited Jan 1 at 10:07









                                                                                                                                                                                                          Sashi

                                                                                                                                                                                                          8312820




                                                                                                                                                                                                          8312820










                                                                                                                                                                                                          answered Jan 1 at 6:40









                                                                                                                                                                                                          jamal bagwanjamal bagwan

                                                                                                                                                                                                          11




                                                                                                                                                                                                          11













                                                                                                                                                                                                          • Welcome to stack overflow. Please also add a description on what the code does instead of just providing the code.

                                                                                                                                                                                                            – Sashi
                                                                                                                                                                                                            Jan 1 at 6:52



















                                                                                                                                                                                                          • Welcome to stack overflow. Please also add a description on what the code does instead of just providing the code.

                                                                                                                                                                                                            – Sashi
                                                                                                                                                                                                            Jan 1 at 6:52

















                                                                                                                                                                                                          Welcome to stack overflow. Please also add a description on what the code does instead of just providing the code.

                                                                                                                                                                                                          – Sashi
                                                                                                                                                                                                          Jan 1 at 6:52





                                                                                                                                                                                                          Welcome to stack overflow. Please also add a description on what the code does instead of just providing the code.

                                                                                                                                                                                                          – Sashi
                                                                                                                                                                                                          Jan 1 at 6:52











                                                                                                                                                                                                          -2














                                                                                                                                                                                                          #! python3
                                                                                                                                                                                                          # Table Printer 1

                                                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                          def printTable(data):
                                                                                                                                                                                                          colWidths = [0] * len(data)
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          colWidths[x] = len(max(data[x], key = len))
                                                                                                                                                                                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                                                                                                                                                                                          print()

                                                                                                                                                                                                          printTable(tableData)


                                                                                                                                                                                                          #! python3
                                                                                                                                                                                                          # Table Printer 2

                                                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                          def printTable(data):
                                                                                                                                                                                                          colWidths = [0] * len(data)
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          if len(data[x][y]) > colWidths[x]:
                                                                                                                                                                                                          colWidths[x] = len(data[x][y])
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                                                                                                                                                                                          print()

                                                                                                                                                                                                          printTable(tableData)





                                                                                                                                                                                                          share|improve this answer



















                                                                                                                                                                                                          • 1





                                                                                                                                                                                                            Hi test, could you develop what you're doing here to answer the question? It's absolutely not clear.

                                                                                                                                                                                                            – J. Chomel
                                                                                                                                                                                                            May 30 '16 at 7:32






                                                                                                                                                                                                          • 1





                                                                                                                                                                                                            Could you please add some context to your code?

                                                                                                                                                                                                            – ppperry
                                                                                                                                                                                                            May 30 '16 at 13:38
















                                                                                                                                                                                                          -2














                                                                                                                                                                                                          #! python3
                                                                                                                                                                                                          # Table Printer 1

                                                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                          def printTable(data):
                                                                                                                                                                                                          colWidths = [0] * len(data)
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          colWidths[x] = len(max(data[x], key = len))
                                                                                                                                                                                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                                                                                                                                                                                          print()

                                                                                                                                                                                                          printTable(tableData)


                                                                                                                                                                                                          #! python3
                                                                                                                                                                                                          # Table Printer 2

                                                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                          def printTable(data):
                                                                                                                                                                                                          colWidths = [0] * len(data)
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          if len(data[x][y]) > colWidths[x]:
                                                                                                                                                                                                          colWidths[x] = len(data[x][y])
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                                                                                                                                                                                          print()

                                                                                                                                                                                                          printTable(tableData)





                                                                                                                                                                                                          share|improve this answer



















                                                                                                                                                                                                          • 1





                                                                                                                                                                                                            Hi test, could you develop what you're doing here to answer the question? It's absolutely not clear.

                                                                                                                                                                                                            – J. Chomel
                                                                                                                                                                                                            May 30 '16 at 7:32






                                                                                                                                                                                                          • 1





                                                                                                                                                                                                            Could you please add some context to your code?

                                                                                                                                                                                                            – ppperry
                                                                                                                                                                                                            May 30 '16 at 13:38














                                                                                                                                                                                                          -2












                                                                                                                                                                                                          -2








                                                                                                                                                                                                          -2







                                                                                                                                                                                                          #! python3
                                                                                                                                                                                                          # Table Printer 1

                                                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                          def printTable(data):
                                                                                                                                                                                                          colWidths = [0] * len(data)
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          colWidths[x] = len(max(data[x], key = len))
                                                                                                                                                                                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                                                                                                                                                                                          print()

                                                                                                                                                                                                          printTable(tableData)


                                                                                                                                                                                                          #! python3
                                                                                                                                                                                                          # Table Printer 2

                                                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                          def printTable(data):
                                                                                                                                                                                                          colWidths = [0] * len(data)
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          if len(data[x][y]) > colWidths[x]:
                                                                                                                                                                                                          colWidths[x] = len(data[x][y])
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                                                                                                                                                                                          print()

                                                                                                                                                                                                          printTable(tableData)





                                                                                                                                                                                                          share|improve this answer













                                                                                                                                                                                                          #! python3
                                                                                                                                                                                                          # Table Printer 1

                                                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                          def printTable(data):
                                                                                                                                                                                                          colWidths = [0] * len(data)
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          colWidths[x] = len(max(data[x], key = len))
                                                                                                                                                                                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                                                                                                                                                                                          print()

                                                                                                                                                                                                          printTable(tableData)


                                                                                                                                                                                                          #! python3
                                                                                                                                                                                                          # Table Printer 2

                                                                                                                                                                                                          tableData = [['apples', 'oranges', 'cherries', 'banana'],
                                                                                                                                                                                                          ['Alice', 'Bob', 'Carol', 'David'],
                                                                                                                                                                                                          ['dogs', 'cats', 'moose', 'goose']]

                                                                                                                                                                                                          def printTable(data):
                                                                                                                                                                                                          colWidths = [0] * len(data)
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          if len(data[x][y]) > colWidths[x]:
                                                                                                                                                                                                          colWidths[x] = len(data[x][y])
                                                                                                                                                                                                          for y in range(len(data[0])):
                                                                                                                                                                                                          for x in range(len(data)):
                                                                                                                                                                                                          print(data[x][y].rjust(colWidths[x]), end = ' ')
                                                                                                                                                                                                          print()

                                                                                                                                                                                                          printTable(tableData)






                                                                                                                                                                                                          share|improve this answer












                                                                                                                                                                                                          share|improve this answer



                                                                                                                                                                                                          share|improve this answer










                                                                                                                                                                                                          answered May 30 '16 at 7:20









                                                                                                                                                                                                          testtest

                                                                                                                                                                                                          1




                                                                                                                                                                                                          1








                                                                                                                                                                                                          • 1





                                                                                                                                                                                                            Hi test, could you develop what you're doing here to answer the question? It's absolutely not clear.

                                                                                                                                                                                                            – J. Chomel
                                                                                                                                                                                                            May 30 '16 at 7:32






                                                                                                                                                                                                          • 1





                                                                                                                                                                                                            Could you please add some context to your code?

                                                                                                                                                                                                            – ppperry
                                                                                                                                                                                                            May 30 '16 at 13:38














                                                                                                                                                                                                          • 1





                                                                                                                                                                                                            Hi test, could you develop what you're doing here to answer the question? It's absolutely not clear.

                                                                                                                                                                                                            – J. Chomel
                                                                                                                                                                                                            May 30 '16 at 7:32






                                                                                                                                                                                                          • 1





                                                                                                                                                                                                            Could you please add some context to your code?

                                                                                                                                                                                                            – ppperry
                                                                                                                                                                                                            May 30 '16 at 13:38








                                                                                                                                                                                                          1




                                                                                                                                                                                                          1





                                                                                                                                                                                                          Hi test, could you develop what you're doing here to answer the question? It's absolutely not clear.

                                                                                                                                                                                                          – J. Chomel
                                                                                                                                                                                                          May 30 '16 at 7:32





                                                                                                                                                                                                          Hi test, could you develop what you're doing here to answer the question? It's absolutely not clear.

                                                                                                                                                                                                          – J. Chomel
                                                                                                                                                                                                          May 30 '16 at 7:32




                                                                                                                                                                                                          1




                                                                                                                                                                                                          1





                                                                                                                                                                                                          Could you please add some context to your code?

                                                                                                                                                                                                          – ppperry
                                                                                                                                                                                                          May 30 '16 at 13:38





                                                                                                                                                                                                          Could you please add some context to your code?

                                                                                                                                                                                                          – ppperry
                                                                                                                                                                                                          May 30 '16 at 13:38





                                                                                                                                                                                                          protected by Community Jan 1 at 6:41



                                                                                                                                                                                                          Thank you for your interest in this question.
                                                                                                                                                                                                          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                                                                                                                          Would you like to answer one of these unanswered questions instead?



                                                                                                                                                                                                          Popular posts from this blog

                                                                                                                                                                                                          Monofisismo

                                                                                                                                                                                                          Angular Downloading a file using contenturl with Basic Authentication

                                                                                                                                                                                                          Olmecas