I have list of lists of strings like this:
[['today'], ['is'], ['rainy'], ['day']]
I want to print each word vertically in python. I want to output something like this
t i r d
o s a a
d i y
a n
y y
I have list of lists of strings like this:
[['today'], ['is'], ['rainy'], ['day']]
I want to print each word vertically in python. I want to output something like this
t i r d
o s a a
d i y
a n
y y
You could try itertools.zip_longest
from itertools import zip_longest
lst = [['today'], ['is'], ['rainy'], ['day']]
print('\n'.join([' '.join([y or ' ' for y in x]) for x in zip_longest(*[i[0] for i in lst])]))
Output:
t i r d
o s a a
d i y
a n
y y
y or ' ' instead of ' ' if y is None else yYou can do this will a loop.
l = [['today'], ['is'], ['rainy'], ['day']]
max_len = len(max(l, key=lambda x: len(x[0]))[0])
out = ''
for idx in range(max_len):
for item in l:
small_item = item[0]
if len(small_item) > idx:
out += small_item[idx]
out += "\t"
else:
out += " \t"
out += "\n"
print(out)
t i r d
o s a a
d i y
a n
y y
Idea behind this is simple. You have to find the length of longest string you have. Then basically you want to pick up each item from each string if that element is valid, else add a empty space. Add tabs and newlines to format as you want.