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.

Author terry.reedy
Recipients george-shuklin, steven.daprano, terry.reedy
Date 2017-02-10.20:13:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1486757605.84.0.0297344388478.issue29511@psf.upfronthosting.co.za>
In-reply-to
Content
Lists, tuples, ranges, dicts, and other builtin collection objects already work with 'in'.

>>> 1 in [1,2,3]
True
>>> 4 in range(9)
True

For historical reasons, stings have both 'find' and 'index'.  The only difference is returning -1 (a C-ism) versus raising ValueError on failure.  They are otherwise redundant.

Lists, tuples, and ranges, and other builtin sequence objects already  have 'index'.  There is no need to repeat the redundancy of 'find'.

>>> [1,2,3].index(2)
1
>>> [1,2,3].index(4)
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    [1,2,3].index(4)
ValueError: 4 is not in list
>>> range(9).index(4)
4

Strings are a special case of collection in that they 'contain' substrings rather than items of a different class.  For this reason, 'in' and index/find are special-cased also work with contiguous substrings of length greater than 1.

>>> 'ac' in 'abc'
False
>>> 'ab' in 'abc'
True

Extending this idea to 'subsequence in sequence' or sequence.index(subsequence) has been rejected.

Note: __cmp__ does not exist in 3.x.  Collection 'in' and sequence 'index' check object identity before equality to guarantee that an object in a collection (in the common sense of the term) is actually found even if it has a screwy definition of __eq__.

>>> nan = float('nan')
>>> nan == nan
False
>>> nan in [nan]
True
>>> float('nan') in [float('nan')]
False
>>> [nan].index(nan)
0
History
Date User Action Args
2017-02-10 20:13:25terry.reedysetrecipients: + terry.reedy, steven.daprano, george-shuklin
2017-02-10 20:13:25terry.reedysetmessageid: <1486757605.84.0.0297344388478.issue29511@psf.upfronthosting.co.za>
2017-02-10 20:13:25terry.reedylinkissue29511 messages
2017-02-10 20:13:25terry.reedycreate