You can create sublist that partition the data you need to print.
Before printing the eacht part, test if you need to print a '\n'
for the previous line and print partitiones without '\n'
:
r, c = map(int, input().split())
# create the parts that go into each line as sublist inside partitioned
partitioned = [ list(range(i+1,i+c+1)) for i in range(0,r*c,c)]
# ^^^^ 1 ^^^^ ^^^^ 2 ^^^^
for i,data in enumerate(partitioned):
if i>0: # we need to print a newline after what we printed last
print("")
print(*data, sep = " ", end = "") # print sublist with spaces between numbers and no \n
^^^^ 1 ^^^^
creates a range of all the numbers you need to print for each partition
^^^^ 2 ^^^^
creates the starting numbers of each "row" thats used in ^^^^ 1 ^^^^
(reduced by 1 but thats fixed in 1's range)
enumerate(partitioned)
returns position inside sequence and the data at that position - you only want to print '\n'
after the first output was done.
After the last partitioned
- output the for ...
is finished and wont enter again - hence no \n after it.
Output for '6 3'
(\n added for claritiy reasons):
1 2 3\n
4 5 6\n
7 8 9\n
10 11 12\n
13 14 15\n
16 17 18
with partitioned
being:
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]