Python Forum
Append only adding the same list again and again
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append only adding the same list again and again
#1
Hi, I'm facing this really weird issue.I've been appending different lists to my list but while i print the result it shows that the last value appended is only added as many times as I appended different lists. My code and output will clear out exactly what I meant.
Here is my code:
def generate_MagicSquare(n):
    mat=[[0 for i in range(n)]for i in range(n)]
    i,j=n//2,n-1
    mat[i][j]=1
    number_inserted=1
    a=n**2
    while number_inserted<a:
        i,j=i-1,j+1
        if i==-1 and j==n:
            i,j=0,n-2
        elif i==-1 or j==n:
            if i==-1:
                i=n-1
            if j==n:
                j=0
        else:
            pass
        if mat[i][j]!=0:
            j-=2
            i+=1
        number_inserted+=1
        mat[i][j]=number_inserted
    return mat

def rotate_Clockwise_90(B):
    n1=len(B[0])
    for i in range(n1//2):
        for j in range(i,n1-i-1):
            temp=B[i][j]
            B[i][j]=B[n1-1-j][i]
            B[n1-1-j][i]=B[n1-1-i][n1-1-j]
            B[n1-1-i][n1-1-j]=B[j][n1-1-i]
            B[j][n1-1-i]=temp
    return B

x=[]
mat=generate_MagicSquare(3)
print(mat)
x.append(mat)
print(x)
a=rotate_Clockwise_90(mat)
print(a)
x.append(a)
print(x)
b=rotate_Clockwise_90(a) #Rotate by 180
print(b)
x.append(b)
print(x)
Output:
Output:
[[2, 7, 6], [9, 5, 1], [4, 3, 8]] [[[2, 7, 6], [9, 5, 1], [4, 3, 8]]] [[4, 9, 2], [3, 5, 7], [8, 1, 6]] [[[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]]] [[8, 3, 4], [1, 5, 9], [6, 7, 2]] [[[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]]] Process finished with exit code 0

I think it has something to do with memory addresses, but don't have a good idea about it.
Any suggestions will be very helpful.
Thank you!
Reply


Messages In This Thread
Append only adding the same list again and again - by viraj1123 - Jun-17-2020, 02:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 461 Mar-14-2024, 06:26 PM
Last Post: flash77
Question How to append integers from file to list? Milan 8 1,443 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  Adding values with reduce() function from the list of tuples kinimod 10 2,631 Jan-24-2023, 08:22 AM
Last Post: perfringo
  read a text file, find all integers, append to list oldtrafford 12 3,511 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 10,322 Jun-12-2022, 06:54 PM
Last Post: Mark17
  Adding a list to Python Emailing Script Cknutson575 4 2,300 Feb-18-2021, 09:13 AM
Last Post: buran
  adding numbers in a list Nickd12 2 2,186 Jan-15-2021, 12:46 PM
Last Post: Serafim
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,313 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Adding List Element if Second part of the List Elements are the Same quest_ 3 2,433 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  How to append to list a function output? rama27 5 6,709 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020