Search code examples
pythonlistline

Print x number of list and then insert new line in python


I have a list of integer like this

[1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

I want to print maybe 10 int in one line, and then insert a new line so the output should looks like this:

[1, 2, 1, 2, 1, 1, 1, 1, 1, 1,

1, 1, 1, 1, 1, 1, 1, 2, 2, 1,

1, 1, 1, 1, 1, 1, 2, 1, 1, 1,

1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

I need to do this because i need to screenshoot the output of the list to my paper, its better if the list look more tidy. Thanks before.


Solution

  • If you dont want to use any external library, here's one way:

    print('['+'\n'.join([','.join(map(str, a[i:i+x])) for i in range(0,len(a),x)])+']')