0

I am using python 2.7.10 from Anaconda 2.2.0, on Ipython Notebook, and am observing the following apparent bug (see code below). The same operation on two equivalent lists produces two unequivalent lists. If it is intended that the two lists are supposed to behave differently because of the different ways that they are initiated, then an equality comparison of the two lists should not evaluate to True (because A == B <=> F(A) == F(B)) . To my mind, this must be a bug either in the list object definition, or the equality operator implementation. Can somebody please confirm that this is a bug and tell me the best way to call this to the community's attention?

> test = [[]]*3 
> testtwo = [[],[],[]]
> print(test)
> print(testtwo)
> print(test==testtwo)

[[], [], []]
[[], [], []]
True    

> test[1].append(2)
> testtwo[1].append(2)
> print(test)
> print(testtwo)
> print(test==testtwo)

[[2], [2], [2]]
[[], [2], []]
False
A. Arpi
  • 217
  • 1
  • 2
  • 6
  • Your two initial lists are *not* equivalent, see the duplicate post. You created one list with three *separate* lists in `testtwo`, but `test` contains 3 references to *one list object*. – Martijn Pieters Jul 16 '16 at 18:14
  • Not a bug. Read this to see how [[]]*3 works: : http://stackoverflow.com/questions/3459098/create-list-of-single-item-repeated-n-times-in-python/3459131 – jdigital Jul 16 '16 at 18:14
  • More generally, if you think you've found a bug in python (or any other language for that matter), you might want to triple check your understanding of how things are supposed to work. Not to say that there are no bugs, but python 2.7 has been around for quite a while and a fundamental bug like this would have been spotted a long time ago. This is a common mistake, I'm sure you won't do it again ;-) – jdigital Jul 16 '16 at 18:18
  • And at the start your two lists *are* **equal** as they have the same value; `test == testtwo` is true because `test[0] == testtwo[0]`, `test[1] == testtwo[1]` and `test[2] == testtwo[2]` are all true. That doesn't mean that the same operation can't have different effects however. `0 == 0.0` in Python, but `isinstance(0, int)` is true but `isinstance(0.0, int)` is not; same value, different objects. – Martijn Pieters Jul 16 '16 at 18:25

0 Answers0