code_junkie

Do I have to cause an ValueError in Python

Nov 11th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. chars = #some list
  2.  
  3. try:
  4. indx = chars.index(chars)
  5. except ValueError:
  6. #doSomething
  7. else:
  8. #doSomethingElse
  9.  
  10. chars = #some list
  11.  
  12. indx = chars.index(chars)
  13.  
  14. if indx == -1:
  15. #doSomething
  16. else:
  17. #doSomethingElse
  18.  
  19. if element in mylist:
  20. index = mylist.index(element)
  21. # ... do something
  22. else:
  23. # ... do something else
  24.  
  25. d = dict((x, loc) for (loc,x) in enumerate(chars))
  26. ...
  27. index = d.get(chars_to_find, -1) # Second argument is default if not found.
Add Comment
Please, Sign In to add comment