find nearest time in datetime list

Boris Borcic bborcic at gmail.com
Wed Jan 30 08:45:08 EST 2008


washakie wrote:
> Hello,
> 
> I have a list of datetime objects: DTlist, I have another single datetime
> object: dt, ... I need to find the nearest DTlist[i]  to the dt .... is
> there a simple way to do this? There isn't necessarily an exact match... 
> 
> Thanks!
> .john
> 

min(DTlist,key=lambda date : abs(dt-date))



More information about the Python-list mailing list