4 ms·
> I, for one, hate throwing an exception when an item isn't found. This sucks: You can always do: index = a_list.index(a_value) if a_value in a_list else
by RyanMcGreal 15y ago
> I, for one, hate throwing an exception when an item isn't found. This sucks:
You can always do:
index = a_list.index(a_value) if a_value in a_list else -1
- ge0rg 15y agoThere is even a built-in for that: >>> "foo".find("a") -1
- andreasvc 15y agoNo there isn't. The one you cite is for strings, not lists. I've always wondered why lists shouldn't also have find, rindex or replace methods.
- anthonyb 15y agoWhy would you do that? You're going to have to have a branch to deal with the -1 further down in your code anyway. Why not just deal with it when it happens: if value not in list: # value not found return index = list.index(value) # Code to deal with the thing