Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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


Why 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


There is even a built-in for that:

    >>> "foo".find("a")
    -1


No 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: