On Fri, Aug 29, 2008 at 7:17 PM, Steven Bethard firstname.lastname@example.org:
On Fri, Aug 29, 2008 at 6:25 PM, Bruce Leban email@example.com wrote:
Not quite: len(items) requires iterating across the entire list which may not be necessary or desirable. Plus you may need an intermediate variable
store it. I think that's inferior.
If len(items) requires iterating across the entire list, then you're not using a Python list object, you're using someone else's (poorly implemented) list data structure. Python lists give len(obj) in O(1).
items need not be a literal list. If you can compute this len([t for t in range(100) if foo(t)]): without iterating over the list, then there's a Turing award waiting for you. :-) (Yes, if you know what foo is perhaps you can optimize that. I'm not counting that.) This is also why you may need an intermediate variable: you don't want to compute that list more than once because foo(t) may have a side effect.