You can use the logic from the grouper recipe:
listA=['a','b','c','d','e','f','g','h','i','j']
print("\n".join(map(" ".join, zip(*[iter(listA)] * 3))))
a b c
d e f
g h i
If you don't want to lose elements use izip_longest with an empty string as a fillvalue:
listA=['a','b','c','d','e','f','g','h','i','j']
from itertools import izip_longest
print("\n".join(map(" ".join, izip_longest(*[iter(listA)] * 3,fillvalue=""))))
Which differs in that it keeps the j:
a b c
d e f
g h i
j
You can put the logic in a function and call it when you want to print, passing in whatever values you want.
from itertools import izip_longest
def print_matrix(m,n, fill):
print( "\n".join(map(" ".join, izip_longest(*[iter(m)] * n, fillvalue=fill))))
Or without itertools just chunk and join, you can also take a sep arg to use as the delimiter:
def print_matrix(m,n, sep):
print( "\n".join(map("{}".format(sep).join, (m[i:i+n] for i in range(0, len(m), n)))))
You just need to pass the list and the size for each row:
In [13]: print_matrix(listA, 3, " ")
a b c
d e f
g h i
j
In [14]: print_matrix(listA, 3, ",")
a,b,c
d,e,f
g,h,i
j
In [15]: print_matrix(listA, 4, ",")
a,b,c,d
e,f,g,h
i,j
In [16]: print_matrix(listA, 4, ";")
a;b;c;d
e;f;g;h
i;j