Can I search a list for a range of values?
Tim Chase
python.list at tim.thechases.com
Fri Oct 14 18:30:25 EDT 2011
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]
-tkc
More information about the Python-list
mailing list