Am trying to create a multidimensional list. Input would be:
L1 = [2,3,4]
L2 = [5,6,7]
Output would be x ** y, where x is from L1 and x is from L2
L3 = [[32,64,128],[243,729,2187],[1024,4096,16384]]
My code is as below:
L1 = [2,3,4]
L2 = [5,6,7]
L3 = [[None] * len(L1)] * len(L2)
c=0
for x in L1:
d=0
for y in L2:
L3[c][d] = x ** y
d = d+1
c = c+1
print L3
Here the output is :
[[1024,4096,16384],[1024,4096,16384],[1024,4096,16384]]
While debugging i got to know:
at the time of assigning to "L3[0][0]" , its assigning values to L3[0][0],L3[1][0],L3[2][0]. It should assign nine times. During each assignment its assigning to 3 places. I am not getting how its working.
I tried to print
id(L3[0])
id(L3[1])
id(L3[2])
and all are printing same memory location address.
Is it because of the same memory address that the value is passed to 3 places at once(the value of x**y is simultaneously going to L3[0][0],L3[1][0],L3[2][0]?
Can somebody help me out of this.
Thanks in advance, SRP