2

I've been trying to print horizontally multiple lines elements from a list with different methods (end= ' ', ' '.join(element), etc) but couldn't manage to do it without the last line of element_1 and the first line of element_2 being overlapped.

I tried this code:

operations = ['    10\n-  214\n------\n  -204', '   5\n+ 12\n----\n  17', '  8887\n-    7\n------\n  8880']
for operation in operations:
    print(operation, end='    ')

and expect to have a layout looking like this: https://i.stack.imgur.com/WBSYM.png

but I got that: https://i.stack.imgur.com/AlKpt.png

Can anyone give me a clue please?

k13
  • 21
  • 1

1 Answers1

1

You need to change order of items in your list

lines = [[] for _ in range(4)]

for operation in operations:
    for i, line in enumerate(operation.split('\n')):
        lines[i].append(line)

this way we get a list of lines as we want to print them. We can do that using

for line in lines:
   print('    '.join(line))
Nejc
  • 220
  • 1
  • 8