Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print a list of numbers vertically?

Tags:

python

I am trying to solve an exercise. I should write a function that takes a list of integers, converts it to a string of numbers displayed vertically.

mylist = [5,69,2090]

the function should return/print the following string:

    2
    0
  6 9
5 9 0

I tried to solve it with the following code, but it doesn't help me

def printstring(mylist):
    h = len(mylist)//3
    for i in range(h):
        print(mylist[i],mylist[h+i],l[h+i+1])
like image 640
alex108 Avatar asked Oct 22 '25 07:10

alex108


1 Answers

First convert your list items to a string then find the maximum length. Pad your strings to fit that length and print them:

lst = [5, 69, 2090]
lst = list(map(str, lst))
max_lenght = max(map(len, lst))
padded_lst = [item.rjust(max_lenght, " ") for item in lst]
for row in zip(*padded_lst):
    print(" ".join(row))
    2
    0
  6 9
5 9 0
like image 135
Asocia Avatar answered Oct 23 '25 20:10

Asocia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!