Print columns side by side in python

Viewed 41

When applying this code: (for example)

x = ["Apple\napple", "Orange\norange", "Banana\nbanana"]
for i in x:
    print(i, end = " ")

The result is:

Apple
apple Orange
orange Banana
banana

But I need a way to make it like this:

Apple Orange Banana
apple orange banana
1 Answers
  • You need to split element of x to y first
  • Then print indiced index of element in y

Try this code below

x = ["Apple\napple", "Orange\norange", "Banana\nbanana"]
y = [x[i].split() for i in range(len(x))]
print(f'{y[0][0]} {y[1][0]} {y[2][0]}\n{y[0][1]} {y[1][1]} {y[2][1]}')
Related