0

I am working on a project called "Print Table" from 'Automate The Boring Stuff with Python' book. And I struggled to print every sublist side by side. Here is the list of items after I equalized characther numbers:

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

Here is how it should look in the end:

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

And here is how I handled it in dumbest way possible:

for x in newtable:
    xi = newtable.index(x)
    for y in x:
        yi = newtable[xi].index(y)
        for z in newtable:
            zi = newtable.index(z)
            print(newtable[zi][yi],end=" ")
        print("\n")
    break

It works but i want to find out how can i make it with less headaches. How could i make it with .join() or sth. else?

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
haintaki
  • 11
  • 2

1 Answers1

3

You can use zip built-in function to produce the expected outcome.

>>> for row in zip(*newtable):
...     print(*row)
...
  apples Alice  dogs
 oranges   Bob  cats
cherries Carol moose
  banana David goose
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46