Checking a string against multiple matches

alex23 wuwei23 at gmail.com
Tue Dec 2 02:01:35 CET 2008


On Dec 2, 5:31 am, Aaron Scott <aaron.hildebra... at gmail.com> wrote:
> I was using .index on the
> list, but it would return True for strings that contained the search
> string rather than match it exactly, leading to false positives in my
> code.

Are you sure? That doesn't seem like standard behaviour.

>>> l = ["one", "two", "three", "four"]
>>> l.index('on')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list
>>> l.index('thre')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list

The only time I'd expect it to do partial matches is if you were doing
string.index(string), rather than list.index(string):

>>> "four".index('our')
1





More information about the Python-list mailing list