Is every number in a list in a range?

Paul Rubin http
Mon Mar 5 22:19:38 EST 2007


"MonkeeSage" <MonkeeSage at gmail.com> writes:
> If you want to preserve the items after 46, then you can't just bail
> when you see 46. You need to keep looking at the following values and
> comparing them against the boundry conditions. If the the input is
> sequential, then this isn't a problem, as all items above 46 are out
> of range....but if the input is arbitrary, then you have to look at
> each index individually as you know. So it really comes down to if the
> data set is ordered or arbitrary as to which approach is more
> efficient.

OK, I didn't read the question that way.  I thought the person just
wanted a yes or no answer to whether the list contained any elements
outside the desired range.  If the purpose is to select the elements
in the range, then use filter/ifilter or a different listcomp/genexp:

   inrange = [x for x in ll if 0<=x<=maxnum]



More information about the Python-list mailing list