3

I have a list called self.nodePathList which contatins [782, 455, 231]. I then put it into another list variable: self.sortedNodePath = self.nodePathList. both lists now contain the same things. I sort self.sortedNodePath. Yet, they both get sorted. i checked through the code over and over but i'm not making any spelling errors or using the wrong names. the code is below if you want to use it

self.sortedNodePath = self.nodePathList

#######sorting

for passnum in range(len(self.sortedNodePath)-1,0,-1):
    for i in range(passnum):
           if self.sortedNodePath[i]>self.sortedNodePath[i+1]:
                  self.temp = self.sortedNodePath[i]
                  self.sortedNodePath[i] = self.sortedNodePath[i+1]
                  self.sortedNodePath[i+1] = self.temp

#######END OF SORTING
Pavilion Sahota
  • 105
  • 1
  • 1
  • 4

3 Answers3

3

In your example, self.sortedNodePath and self.nodePathList both point to the same object. When you sort that object, both variables are still pointing to it.

You need to create a new object by using:

self.sortedNodePath = list(self.nodePathList)

As an example:

foo = ['test', 'foo']
bar = foo

print id(foo)
>>> 23367192

print id(bar)
>>> 23367192   # You can see both foo and bar have the same object ID

bar = list(foo)

print id(bar)
>>> 23387392  # Now using list(), bar has a separate object ID from foo
MrAlexBailey
  • 5,219
  • 19
  • 30
  • See [this post](http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list-in-python) for more information. I never noticed it before but it appears as though using `list()` is a faster alternative. – MrAlexBailey Mar 06 '15 at 17:24
  • The usual way to copy a list is `new = orig[:]`, since `list(orig)` can have unintended side effects if an object is sliceable but not ACTUALLY a list to begin with (e.g. a subclass of a list, or a string or etc) – Adam Smith Mar 06 '15 at 17:49
  • Those unintended side effects would be a good thing, Python is meant to be very explicit, and creating a new ID using `list()` doesn't get any more explicit. Slicing with `new = orig[:]` could for example be used to copy a tuple into a new tuple. Which may not be intended. If you are simply trying to copy a list then any errors received from `list()` should be considered a very useful and relevant hint from Python that you may need to reconsider what you are working with. – MrAlexBailey Mar 06 '15 at 17:56
0

You're not creating two lists, you're creating two references to the same list.

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
0

If you check the ids of those two list objects with print id(self.sortedNodePath) and print id(self.NodePathList) you will see that they are both, in reality, the same list.

To make a real copy:

 self.sortedNodePath = []
 self.sortedNodePath.extend(self.nodePathList)

Also, note that in Python one usually sorts a list with sorted(list) or perhaps list.sort(). If the items in your list are not simple types that Python knows how to compare, you can supply a function to both sorted() and list.sort() that will be used to extract a comparison value for each object in the list.

Larry Lustig
  • 49,320
  • 14
  • 110
  • 160