Anonymous asked in Computers & InternetProgramming & Design · 3 weeks ago

PYTHON 3 HELP: How to print elements at same indices in a list of lists side by side?

This is my list of lists:

lst = [[1, 3, 4], [2, 5, 7], [9, 8, 7]]. The output that I want is an in the png below. Please note that what is printed should be a string, and that there is a space in between every number.

Please help!


***just to clarify what I want is NOT a png. I just want the ouput to look like what is in the png

Attachment image

2 Answers

  • EddieJ
    Lv 7
    3 weeks ago
    Favorite Answer

    You can try this:

    for i in range (len (lst)):

       for row in lst: print (row [i], end=" ")

       print ()

    However, one number is different, and if the .png is correct, then I don't understand what it represents, or maybe your original list has a typo.

    • A conscience
      Lv 6
      3 weeks agoReport

      Note that this assumes the list of list is a square array. It would not work for a general list of lists.

  • 3 weeks ago

    Update: I just noticed you are trying to print the tranposed list

    _ = [print(*x) for x in zip(*lst)]


    for col in zip(*lst):

       print(''{} {} {}".format(*col))


    Old Answer


    Hacky way:

    _ =[print(*x) for x in lst]

    More clear way:

    for row in lst:

        print(''{} {} {}".format(*row))

    • Sara3 weeks agoReport

       is there an alternative way instead of using zip?

Still have questions? Get your answers by asking now.