The way I would consider approaching this is recognizing that you are generating a list of lists, as opposed to a 2D array.
Also, given your apparent level of experience I think a procedural approach might be a good start for you.
# Your given information
row = 6
col = 10
# Initialize an empty list of lists
lst = []
# Continuously append lists by converting range() objects
i = 1
while i <= (row * col):
next_i = i + col
arr.append(list(range(i, next_i)))
i = i + next_i
Some good next steps would be to investigate the documentation for range() and list(). At first glance they look like functions but they are actually an immutable and a mutable (respectively) sequence type
Note: A comment on your question says
You happen to have created a list with a bunch of references to the same inner list.
To illustrate this, observe what happens in the python interactive interpreter when I try to modify (mutate) one element of the list of lists:
>>> row = 6
>>> column = 10
>>> lst = row*[column*[0]]
>>> lst
[[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, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> lst[0][0] = 999 # set just row 0, column 0 to 999
>>> lst
[[999, 0, 0, 0, 0, 0, 0, 0, 0, 0], [999, 0, 0, 0, 0, 0, 0, 0, 0, 0], [999, 0, 0, 0, 0, 0, 0, 0, 0, 0], [999, 0, 0, 0, 0, 0, 0, 0, 0, 0], [999, 0, 0, 0, 0, 0, 0, 0, 0, 0], [999, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
The result is that the first column of every row has been changed to 999. This is because when you "multiplied" the list
created by [column*[0]]
by row
, what actually happened is you got row
instances of the same list
. So when you refer to one of them, you refer to all of them, in this way. Be careful with this kind of "list multiplication".