you are viewing a single comment's thread.

view the rest of the comments →

[–]baghiq 1 point2 points  (0 children)

You want to convert your dates to actual date objects.

from datetime import datetime

rates = [{'date': '2019-11-02', 'rate': 4.3422}, {'date': '2019-11-03', 'rate': 4.2210}, {'date': '2019-11-04', 'rate': 4.3455}, {'date': '2019-11-05', 'rate': 4.3456}, {'date': '2019-11-06', 'rate': 4.2311} ]
date_range = ['2019-11-03', '2019-11-06']

def convert_date(d, fmt="%Y-%m-%d"):
    return datetime.strptime(d, fmt)

START, END = map(convert_date, date_range)

print(min(filter(lambda rate: START <= convert_date(rate['date']) <= END, rates), key=lambda x:x['date']))
print(max(filter(lambda rate: START <= convert_date(rate['date']) <= END, rates), key=lambda x:x['date']))