Can I search a list for a range of values?

Westley Martínez anikom15 at gmail.com
Sat Oct 15 02:04:12 EDT 2011


On Fri, Oct 14, 2011 at 05:01:04PM -0600, Ian Kelly wrote:
> On Fri, Oct 14, 2011 at 4:30 PM, Tim Chase
> <python.list at tim.thechases.com> wrote:
> > On 10/14/11 17:20, Chris Angelico wrote:
> >>
> >> Try a list comprehension:
> >>
> >>>>> a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]
> >>>>> [i for i in a if i>=10 if i<=20]
> >>
> >> [10, 20, 15, 13, 14]
> >
> > The double-if is new to me.  I thought it was an error when I first saw it,
> > but it seems to be legit syntax (or at least one that 2.7 tolerates,
> > intentionally or otherwise...).  I think I'd make it clearer with either
> >
> >  [i for i in a if i>=10 and i<=20]
> >
> > or even more clearly:
> >
> >  [i for i in a if 10 <= i <= 20]
> 
> As long as we're nitpicking, I'll point out that "i" is an
> inappropriate variable name here, since it is normally used to denote
> indices, not data.  That's why I used "x" in my response instead. ;-)
> 

O that's what i stands for.  I always thought it was integer o_O



More information about the Python-list mailing list