Can I search a list for a range of values?

Tim Chase vim at tim.thechases.com
Fri Oct 14 19:45:12 EDT 2011


On 10/14/11 18:36, Troy S wrote:
> 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'.

Since strings are comparable, you certainly can:

   keys = [k for k in d.keys() if '20110809' < k < '20110911']

-tkc






More information about the Python-list mailing list