Home > Blockchain >  How to skip a line in a python nested list
How to skip a line in a python nested list

Time:10-01

I was trying to make a code in pyh=thon, which contain a nested list. Each list will be in a different line.

line1=list((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
line2=list((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
arena=list()
arena.append(line1)
arena.append('/n')
arena.append(line2)

However the outpoot is [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], '\n', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

How to make it to skip a line?

CodePudding user response:

You can print each item of a list on a separate line by specifying the 'sep', or separator, parameter of print(). By default, this parameter is a space, but we can set it to '\n':

line1=list((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
line2=list((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))

arena=list()
arena.append(line1)
arena.append(line2)    

# Print unpacked list with separator parameter set to '\n'
print(*arena, sep='\n')

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  • Related