Python Forum
Unwanted variable change in module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unwanted variable change in module
#1
I have a module that takes 2 lists and creates a new list from them, but somehow it manages to change one of the input lists when I don't want it to:
def repeatAddCode(newList, oldList):

# Version:  2.0 D Higgins 07/05/2018
# Input:    a) A List of lists of the latest code numbers.
#           b) A List of lists of the original raw code numbers.
# Function: If the last two numbers in a code from newList equal
#           the first two numbers in a code from oldList, add another
#           level of extended numbers.
# Output:   A new list of codes.

    returnList = []
    
# The position of the first column in a code row is stored as Lx[-2]
# and the position of the last column in a code row is stored as Lx[-1].
    for new in newList:
        newStart, newStop = new[-2], new[-1]
        for old in oldList[:]:
            oldStart, oldStop = old[-2], old[-1]
            if oldStart == newStart + 1 and old[oldStop - 1] == new[newStop] and old[oldStop - 2] == new[newStop - 1]:
                for entry in oldList:
                    print '#', entry
                new[newStop + 1] = old[oldStart + 2] # This line changes oldList
                for entry in oldList:
                    print '##', entry

    return returnList
What am I doing wrong?
Reply
#2
You are changing the content of new, that is a list inside newList... and it might be what you whant to do (although your returnList is empty)
I suspect that when you have created the newList in the code that call your repeatAddCode you are doing something like:
newList = oldList[:]
And that create a new "list wrapper" but the id of all the sublist are the same.
To confirm it you can use:
print([id(new) for new in newList])
print([id(old) for new in oldList])
If some id are equal, whatever you do to that element in newList will also happen in oldList.

If you want to create a full copy of the list take a look to the copy module and the deepcopy operation.
Reply
#3
Many thanks for the reply.
In my main program I had the line:
next1 = old1[:]
before I called the module, which I thought would be OK. copy.deepcopy() solves the problem.


Regards, Danny
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 172 7 hours ago
Last Post: deanhystad
Question Unwanted execution of unittest ThomasFab 9 2,050 Nov-15-2022, 05:33 PM
Last Post: snippsat
  Removing the unwanted data from a file jehoshua 14 4,061 Feb-01-2022, 09:56 PM
Last Post: jehoshua
  HELP on Unwanted CSV Export Output | Using Selenium to Scrape soothsayerpg 0 1,269 Jun-13-2021, 12:23 PM
Last Post: soothsayerpg
  how can a variable change if I haven't changed it? niminim 5 3,042 Apr-07-2021, 06:57 PM
Last Post: niminim
  Change variable value during a while loop? penahuse 2 4,049 Nov-15-2020, 11:53 PM
Last Post: penahuse
  Change variable in an outside file ebolisa 5 2,622 Nov-11-2020, 04:41 AM
Last Post: ebolisa
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,253 Aug-12-2020, 10:34 PM
Last Post: snippsat
  Change name of variable samuelbachorik 2 2,049 Aug-10-2020, 02:34 PM
Last Post: deanhystad
  How to eliminate unwanted spaces Mohan 5 2,875 Jun-04-2020, 08:34 AM
Last Post: buran

Forum Jump:

User Panel Messages

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