This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: list.index and rest of list methods disagree if a value is in the list if it's mutated during the call
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Devin Jeanpierre, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2015-01-09 07:25 by Devin Jeanpierre, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg233721 - (view) Author: Devin Jeanpierre (Devin Jeanpierre) * Date: 2015-01-09 07:25
>>> class AppendOnUnequal(object):
...     def __init__(self, append_to):
...         self.append_to = append_to
...     def __eq__(self, other):
...         if self is other:
...             return True
...         self.append_to.append(self)
...         return False
... 
>>> L = [1]; AppendOnUnequal(L) in L
True
>>> L = [1]; L.count(AppendOnUnequal(L))
1
>>> L = [1]; L.remove(AppendOnUnequal(L))
>>> L = [1]; L.index(AppendOnUnequal(L))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: <__main__.AppendOnUnequal object at 0x7f2562d071d0> is not in list


.index() is the only odd one out here. Looks like a bug to me.
msg233723 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-01-09 07:59
This is a non-guaranteed behavior.  It is allowed to be different from other list methods.  The behavior is also very old, stable, and has not been a problem in practice.  No good would come from changing it.
msg233725 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-01-09 08:07
This is because list.index() has start and stop parameters, and L.index(x) is equivalent to L.index(x, 0, len(L)). In list.count() and list.remove() the limit is dynamic during iteration, but in list.index() it is specified by arguments before iterating. It is possible to make the limit static in list.count() and list.remove() or to make it floating if stop is absent or negative in list.index(), but this will complicate and slow down the code without good reason. I suggest to close this issue as "wont fix".
History
Date User Action Args
2022-04-11 14:58:11adminsetgithub: 67393
2015-01-09 08:07:29serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg233725
2015-01-09 07:59:28rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg233723

resolution: not a bug
2015-01-09 07:25:48Devin Jeanpierrecreate