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: Bug in methods of creating the 2d list
Type: Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: samrajput1143, steven.daprano
Priority: normal Keywords:

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=[[0]*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=[[0]*A]*A and getting wrong output . A is positive integer.

##If i'm using for i in range(A-1):
B.append([0]*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=[[0]*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=[[0]*A]*A and getting wrong output . A is positive integer.

##If i'm using for i in range(A-1):
B.append([0]*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) * (Python committer) 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 [[0]]*5 does not make five different sublists, but the same list repeated five times. Exactly the same as this:

    a = [0]
    mylist = [a, a, a, a, a]
msg386608 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) 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

See also:

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 = [[0]]*2
    mylist[0][0] = 1
    print(mylist)  # expect [[1], [0]] but get [[1], [1]]

Thank you.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87323
2021-02-07 20:50:05steven.dapranosetmessages: + msg386608
2021-02-07 20:35:58steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg386606

resolution: not a bug
stage: resolved
2021-02-07 19:12:10samrajput1143setmessages: + msg386603
2021-02-07 19:10:15samrajput1143settitle: Bug in methods of creating the list -> Bug in methods of creating the 2d list
2021-02-07 19:08:50samrajput1143create