4 ms·
A few simple timings tell me this isn't actually fast in Python for finding an item in a list, compared to a bounded loop, (and, since I was curious, neither is
by arc-in-space 5y ago
A few simple timings tell me this isn't actually fast in Python for finding an item in a list, compared to a bounded loop, (and, since I was curious, neither is a sentinel approach where you insert the expected item at the end of the given list), I guess CPython just heavily optimizes range-for loops, and maybe this was true in an old version of it.
(Yes, list.index exists. I know. Thank you.)
- ggrrhh_ta 5y agoThat is why I put the word "general"; C could be a dict, etc. I know that there are better ways for specific types, but the following code is a snippet that works for fast prototyping and I have found it to be idiomatic in VB for collections (but I do very occasional VB, less than once a year). # pythonic pseudocode def exists(x, C): try: C[x] except: # could be more specific # IndexError, KeyError, etc... return False return True