Two dimensional array

Write a Python program which takes two digits as input and generates a two dimentional array.

row = int(input('Enter the number of row :'))
col = int(input('Enter the number of columns : '))
multi_list = [[0 for col in range(col)], [0 for row in range(row)]]

for i in range(row):
     for j in range(col):
            multi_list[row][col] = row * col

print(multi_list)

#Error message : Syntax Error: invalid syntax (for i in range(row))

line 3 is missing a ]

I changed the code according to your suggestion. Now it is half working !!! The present error message is :
Traceback (most recent call last):
File “C:/Users/Administrator/PycharmProjects/pythonProject108/main.py”, line 9, in
multi_list[row][col] = row * col
IndexError: list index out of range

Process finished with exit code 1

multi_list has only two elements, so once you have multi_list[2] I think it gives that error

Actually line 3 had a , [ to much ^^

Dear friends, With the help of an experienced programmer, I solved the two dimensional array problem. I omitted to mention one of the instructions in the question paper "The element value in the i-th row and j-th column of the array should be i*j "
Extremely sorry for the omission. Actually I could not understand the revised code:
but it is working nicely. An explanation is requested from those who understood it.

row_num = int(input('Enter the number of row :'))
col_num = int(input('Enter the number of columns : '))
multi_list = [[0 for i in range(col_num)]  for j in range(row_num)]

for i in range(row_num):
     for j in range(col_num):
            multi_list[i][j] = i * j

print(multi_list)

You could get rid of the for loop by simply telling the generator to use i*j instead of 0.

Also is that the part you don’t understand?
arr = [num for num in range(5)]
Generates an array [0,1,2,3,4].
It’s short for

arr = []
for num in range(5):
  arr.append(num)

The third line in the revised code is the tricky one to under stand .This is that one. “multi_list = [[0 for i in range(col_num)] for j in range(row_num)]” I think it is a nested array. The inside array as a whole replaces ‘0’ in the second part of the main array. This is my understanding.

This line “generates” a nested array.
What is written before the “for” will be generated according to the following loop-condition as if it was in a for-loop.

So the “outside” is on the right.
The outer generator executes row_num times, putting it’s output into the outer array. It’s output is the inner generator, which executes col_num times and turns it’s output into an array. The inner generator outputs just zeros, hence the inner arrays have col_num times “0”. The outer generator calls the inner generator row_num times, resulting in row_num nested arrays filled with col_num zeros.

If you replace the 0 with i*j, it would instead create the arrays with the product of whatever i and j have as values during their call in the generator-loops.

I recommend testing generators without nesting, with nesting and with different values to get a better understanding of how they work :wink:

Hai jagaya, I experimented with one of your suggestions.

No changes in the result. The result is the following two-dimensional array.
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
and I am planning to experiment with other suggestion too.

Ofcourse not - this change was meant so you do not need to write the addition nested for-loops after initially creating multi_list.