I have a list of pairs to convert to strings (for later printing) and I want to insert a \n every three pairs. I could do as in my following sample code, but is there a more compact way in Python?
pairs = [[1,2] for i in range(10)] #my data
pairs = [str(p) + ', ' for p in pairs]
for i in reversed(range(0, len(pairs), 3)):
if i == 0:
continue
pairs.insert(i, '\n')
pairs = ''.join(pairs)
pairs = pairs [:-2] #removing the last ', '
This way, I get:
>>> print pairs
[1, 2], [1, 2], [1, 2],
[1, 2], [1, 2], [1, 2],
[1, 2], [1, 2], [1, 2],
[1, 2]