2

What mechanism is used that allows the built-in function list.remove but not simply list.find?

If I have a list l = [a,b,c...] and want to remove an element, I don't need to know its index, I simply input l.remove(element). Why is it then that I can't use a similar command to find an element's index or to simply check if it's in the list?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 3
    Lists have `l.index(obj)` and `obj in l` which do exactly what you describe. – jonrsharpe May 20 '14 at 22:06
  • This question isn't aware of the `list.index` method, and instead is looking for `list.find`, so I don't think it's a dupe. – Russia Must Remove Putin May 20 '14 at 22:26
  • @AaronHall the question *"Why is it then that I can't use a similar command to find an element's index"* is precisely answered by the duplicate. Also, the OP could use their IDE or [RTFM](https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange) to find the `list` methods. – jonrsharpe May 21 '14 at 07:20
  • "Usually 2 similar questions that hold value for different reasons." and direction to close is only for "exact duplicate." http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled – Russia Must Remove Putin May 21 '14 at 13:01
  • @AaronHall under those criteria, what value does this question hold? – jonrsharpe May 21 '14 at 14:28
  • People looking for `list.find` won't find the other answer, but they will find this one. I think it's worth keeping around. – Russia Must Remove Putin May 21 '14 at 14:58

1 Answers1

2

Interesting that it's not list.find, but list.index:

>>> l = ['a', 'b', 'c']
>>> l.index('c')
2

To test for membership:

>>> 'b' in l
True

Which is equivalent to (and should be used instead of):

>>> l.__contains__('b')
True
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331