Can I search a list for a range of values?

Tim Chase python.list at tim.thechases.com
Fri Oct 14 19:24:13 EDT 2011


On 10/14/11 18:01, Ian Kelly wrote:
> On Fri, Oct 14, 2011 at 4:30 PM, Tim Chase
>> 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. ;-)

Depending on your historical programming-language baggage, "i" is 
usually either an index or integer data, and since the source was 
a list of integers, "i" didn't seem inappropriate.  Same for 
other common data-types:

   [f for f in (1.1, 2.2, 3.3) if 2.0 <= f < 3.0]
   [s for s in ("cat", "hat", "mat") if "bat" < s < "fat"]
   [c for c in "hello, world!" if 'a' <= c <= 'z']

-tkc






More information about the Python-list mailing list