Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.
Note: i=0,1.., X-1; j=0,1,¡Y-1.
Example
Suppose the following inputs are given to the program:
3,5
Then, the output of the program should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
My program:
x = int(raw_input())
y = int(raw_input())
l=[]
for i in range(x):
inner_list = []
for j in range(y):
inner_list.append((i*j))
l.append(inner_list)
print l
Instead of defining 'inner_list' inside the 'for i', if I define it outside as in:
x = int(raw_input())
y = int(raw_input())
l=[]
inner_list = []
for i in range(x):
for j in range(y):
inner_list.append((i*j))
l.append(inner_list)
I am not able to understand the output:
Output:
[0] /inner_list
[0, 0] /inner_list
[0, 0, 0] /inner_list
[[0, 0, 0]] /l
[0, 0, 0, 0] /inner_list
[0, 0, 0, 0, 1] /inner_list
[0, 0, 0, 0, 1, 2] /inner_list
[[0, 0, 0, 0, 1, 2], [0, 0, 0, 0, 1, 2]] /l
[0, 0, 0, 0, 1, 2, 0] /inner_list
[0, 0, 0, 0, 1, 2, 0, 2] /inner_list
[0, 0, 0, 0, 1, 2, 0, 2, 4] /inner_list
[[0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4]] /l
Why the final output is like above
[[0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4]] /l
and not
[[0,0,0],[0,0,0,0,1,2],[0,0,0,0,1,2,0,2,4]]
What I have tried:
I have tried to everything, but no luck.