Skip to content Skip to sidebar Skip to footer

Printing Multiple Lists Vertically?

Good afternoon, I am trying to print 8 lists veritcally and can't seem to find the right formatting. I know you can print a list vertically by... for x in list: print(x) Howev

Solution 1:

Use zip to rotate the lists

for l inzip(*tableaus):
    print(*l) 

Without zip, you could do something like

for i in len(tableaus[0]):
     print(' '.join([l[i] for l in tableaus]))

Solution 2:

Maybe this code solves your problem. It also works if lists are of a different length.

tableaus = [
    ['a', 'b', 'c'],
    [1, 2, 3, 4, 5]
]
tableas_empty = [Falsefor tableau in tableaus]

separator = ' '
empty_value = ' 'whilenotall(tableas_empty):
    row = []
    for i, item inenumerate(tableaus):
        if item:
            value = item.pop(0)
            row.append(str(value))
        else:
            tableas_empty[i] = True
            row.append(empty_value)
    separator.join(row)

Result:

'a 1''b 2''c 3''  4''  5'

Post a Comment for "Printing Multiple Lists Vertically?"