#   This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Type: Bug in methods of creating the 2d list resolved Python 3.8
process
Status: Resolution: closed not a bug samrajput1143, steven.daprano normal

Created on 2021-02-07 19:08 by samrajput1143, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg386602 - (view) Author: sandeep kumar (samrajput1143) Date: 2021-02-07 19:08
```def generateMatrix(A):
d=A-1
data=1
B=[*A]*A
for l in range(int(A/2)):
i=l
j=l
for a in range(d):
B[i][j]=data
print("{0}:{1}:{2}".format(i,j,B[i][j]),end=" ")
data+=1

j+=1
print()
for a in range(d):
B[i][j]=data
print("{0}:{1}:{2}".format(i,j,B[i][j]),end=" ")
data+=1
i+=1
print()
for a in range(d):
B[i][j]=data
print("{0}:{1}:{2}".format(i,j,B[i][j]),end=" ")
data+=1
j-=1
print()
for a in range(d):
B[i][j]=data
print("{0}:{1}:{2}".format(i,j,B[i][j]),end=" ")
data+=1
i-=1
print()
d=d-2
print(d)
print(B)
if d==0:
B[int(A/2)][int(A/2)]=data
print(B[int(A/2)][int(A/2)])
print(B)
return B

##if i'm using different approach to create 2d list . Then i'm getting right output .
in above code i used B=[*A]*A and getting wrong output . A is positive integer.

##If i'm using for i in range(A-1):
B.append(*A)
then i'm getting correct output.

but the 2d list generated using both the approach are same.
2d list from approach 1== 2d list from approach 2 ------->it is true```
msg386603 - (view) Author: sandeep kumar (samrajput1143) Date: 2021-02-07 19:12
```def generateMatrix(A):
d=A-1
data=1
B=[*A]*A
for l in range(int(A/2)):
i=l
j=l
for a in range(d):
B[i][j]=data
print("{0}:{1}:{2}".format(i,j,B[i][j]),end=" ")
data+=1

j+=1
print()
for a in range(d):
B[i][j]=data
print("{0}:{1}:{2}".format(i,j,B[i][j]),end=" ")
data+=1
i+=1
print()
for a in range(d):
B[i][j]=data
print("{0}:{1}:{2}".format(i,j,B[i][j]),end=" ")
data+=1
j-=1
print()
for a in range(d):
B[i][j]=data
print("{0}:{1}:{2}".format(i,j,B[i][j]),end=" ")
data+=1
i-=1
print()
d=d-2
print(d)
print(B)
if d==0:
B[int(A/2)][int(A/2)]=data
print(B[int(A/2)][int(A/2)])
print(B)
return B

#This code is for generating spiral matrix

##if i'm using different approach to create 2d list . Then i'm getting right output .
in above code i used B=[*A]*A and getting wrong output . A is positive integer.

##If i'm using for i in range(A-1):
B.append(*A)
then i'm getting correct output.

but the 2d list generated using both the approach are same.
2d list from approach 1== 2d list from approach 2 ------->it is true```
msg386606 - (view) Author: Steven D'Aprano (steven.daprano) * Date: 2021-02-07 20:35
```This is not a bug, it is working correctly, as designed.

List multiplication does not *copy* the list, it replicates the reference to the same list object. So []*5 does not make five different sublists, but the same list repeated five times. Exactly the same as this:

a = 
mylist = [a, a, a, a, a]```
msg386608 - (view) Author: Steven D'Aprano (steven.daprano) * Date: 2021-02-07 20:50
```There is also an FAQ about this:

https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list

https://docs.python.org/3/faq/programming.html#why-did-changing-list-y-also-change-list-x

By the way, for future bug reports, please take the time to make a *minimal* example please:

http://www.sscce.org/

https://stackoverflow.com/help/minimal-reproducible-example

In this case a minimal example would be:

mylist = []*2
mylist = 1
print(mylist)  # expect [, ] but get [, ]

Thank you.```
History
Date User Action Args