Can I search a list for a range of values?

Troy S tdsimpson at gmail.com
Fri Oct 14 19:36:27 EDT 2011


Can something like this be done with dictionarys?

For example, these are the keys in the dictionary from the call: dict.keys()

['20110601', '20110604', '20110608', '20110611', '20110615',
'20110618', '20110622', '20110625', '20110629', '20110702',
'20110706','20110709', '20110713', '20110716', '20110720', '20110723',
'20110727', '20110730', '20110803', '20110806', '20110810','20110813',
'20110817', '20110820', '20110824', '20110827', '20110831',
'20110903', '20110907', '20110910', '20110914','20110917', '20110921',
'20110924', '20110928', '20111001', '20111005', '20111008']

Is there a way to find all items between '20110809' and '20110911'?
So the subset would be:
['20110810','20110813', '20110817', '20110820', '20110824',
'20110827', '20110831', '20110903', '20110907', '20110910']

The keys are strings that represent a date in the format: 'YYYYMMDD'.

Thanks,

On Fri, Oct 14, 2011 at 6:24 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Fri, Oct 14, 2011 at 4:10 PM, MrPink <tdsimpson at gmail.com> wrote:
>> I have a list like so:
>>
>> a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14]
>>
>> I would like to get a subset from the list with value between 10 and
>> 20 (inclusive).
>>
>> b = [10,13,15,14,20]
>>
>> Is there a way to do this with a list or other data type?
>
> Use a list comprehension:
>
> b = [x for x in a if 10 <= x <= 20]
>
> Cheers,
> Ian
>



-- 
Troy S



More information about the Python-list mailing list