Can I search a list for a range of values?

Chris Angelico rosuav at gmail.com
Fri Oct 14 18:48:05 EDT 2011


On Sat, Oct 15, 2011 at 9:30 AM, Tim Chase
<python.list at tim.thechases.com> wrote:
> 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
>
>

Yeah, it's legal because you can nest fors and ifs in a list comp.
Stylistic difference, I used "if" instead of "and" because there's no
way that it could be a bitwise and. (It's a cross-language safety net
that I sometimes use.) Although the 10<=i<=20 option is definitely
superior to both.

ChrisA



More information about the Python-list mailing list