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
#2
This is because your functions like rotate_clockwise_90 isn't returning a copy of the list, it's modifying the list and then handing it back. So you only have one list that you're modifying over and over again. And your big list just has that one appended multiple times.

You should decide if your functions should be returning copies, or if they should be modifying the passed-in function. Then either the caller or function can be making a copy.

outer_list = []
sublist = ['a']   # list brackets here make a new list
outer_list.append(sublist)
sublist[0] = 'b'  # modifying an element of the list, not a new list
outer_list.append(sublist)
print(outer_list)

# If you don't want to modify the passed-in list, make a copy
sublist = sublist.copy()
sublist[0] = 'c'  # This is modifying only the new list, not the old one
outer_list.append(sublist)
print(outer_list)
Output:
[['b'], ['b']] [['b'], ['b'], ['c']]
Reply
#3
Thank you very much for your response. But I couldn't clearly understand. If possible could you please edit my code so that I can get a proper understanding of what's happening.
Reply
#4
Let me try a different way.

Line 37 creates a new square, and line 39 puts a link to that square into x.
Line 41 is the interesting bit. rotate_clockwise_90 is handed the square and it changes it. This change affects the reference to it in x. So x changes.
Line 43 then puts another link to the square in x. But this is the same link so all the elements in x are identical.

I think a good approach here is to make your functions not modify the data that is passed in That's easily done by making a copy early in the function. This approach is less memory efficient and would be a drawback for larger and more complex manipulations. But it does make them feel more like functions, where you don't expect the input to be modified.

For instance consider changing
def rotate_Clockwise_90(B):
    n1=len(B[0])
    ...
to

import copy
[...]
def rotate_Clockwise_90(B):
    B = copy.deepcopy(B)
    n1=len(B[0])
    ...
It's not the only way to go. You could instead make the copies in the main logic, allowing the functions to continue modifying the data. Either way is a possible solution.
Reply
#5
That works. Thank you very much. Learnt a new thing today!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding a list to Python Emailing Script Cknutson575 4 432 Feb-18-2021, 09:13 AM
Last Post: buran
  adding numbers in a list Nickd12 2 426 Jan-15-2021, 12:46 PM
Last Post: Serafim
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 488 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Adding List Element if Second part of the List Elements are the Same quest_ 3 525 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  How to append to list a function output? rama27 5 836 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  Append list into list within a for loop rama27 2 720 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 683 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  adding elements to a list that are more than a specific number Olavv 2 723 Mar-19-2020, 06:05 PM
Last Post: Olavv
  Adding elements to a list by number Olavv 4 848 Mar-08-2020, 11:16 AM
Last Post: ndc85430
  Problem with append list in loop michaelko03 0 631 Feb-16-2020, 07:04 PM
Last Post: michaelko03

Forum Jump:

User Panel Messages

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