[Tutor] finding numbers in range of of numbers

Kent Johnson kent37 at tds.net
Tue Oct 21 15:35:38 CEST 2008


On Tue, Oct 21, 2008 at 8:47 AM, Richard Lovely
<roadierich at googlemail.com> wrote:
> Guessing you mean [5,100] as the inclusive interval notation, so all
> but the last element of the example pass?
>
> if any(True for x, y in listoflists if 5 <= x and y <= 100):
>   #do stuff

or
if any((5 <= x and y <= 100) for x, y in listoflists):

> You can be slightly faster (but less readable) by changing the
> if any(...):
> to
> if [...]:

Why faster? any() will short-circuit - it will stop processing as soon
as it finds a True item. It also avoids building the entire list. I
would expect it to be faster.

Of course, if you really care about speed you should test, not guess.

Kent


More information about the Tutor mailing list