Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Python
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:
Python
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.
Posted
Updated 21-Jan-17 6:48am
v3

Your output already tells you everything
Quote:
[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

When you add inner_list to l, you just add a pointer to inner_list, you don't copy it. With the debugger, you would see l grow every times you add to inner_list.
----------
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[Update]
Quote:
When I am appending the 'inner-list' to 'l', at the very end why I am getting 3 copies of inner_list as the final value of 'l'
[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
and why not
[[0, 0, 0],[0, 0, 0, 0, 1, 2],[0, 0, 0, 0, 1, 2, 0, 2, 4]] as the final value of 'l'.
This is the way Python works.
When you add in l, you don't copy the list, you add an active link to l.
 
Share this answer
 
v4
Comments
[no name] 21-Jan-17 11:19am    
No not an answer. Neutral 4 after recognizing effort
Patrice T 21-Jan-17 16:17pm    
4 is prefect for me.
Just improved.
Member 12961167 23-Jan-17 2:49am    
When I am appending the 'inner-list' to 'l', at the very end why I am getting 3 copies of inner_list as the final value of 'l'

[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

and why not
[[0, 0, 0],[0, 0, 0, 0, 1, 2],[0, 0, 0, 0, 1, 2, 0, 2, 4]] as the final value of 'l'.

I have gone through the link: http://stackoverflow.com/questions/27821568/how-does-append-method-work-in-python
and http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference to understand the same.

I am new to Python programming, so it would be convenient if anyone can explain step by step. Thanks a lot!!!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900