Skip to content
Related Articles

Related Articles

Improve Article

Python – Find the closest date from a List

  • Last Updated : 09 May, 2021

Given a date and list of dates, the task is to write a python program to find the nearest date in the given input list of dates to the input date.

Examples:

Input : test_date_list = [datetime(2020, 4, 8), datetime(2016, 8, 18), datetime(2018, 9, 24), datetime(2019, 6, 10), datetime(2021, 8, 10)], test_date = datetime(2017, 6, 6)

Output : 2016-08-18 00:00:00

Explanation : Nearest date to 6 June 2017 is 18 August 2016 in list.



Input : test_date_list = [datetime(2020, 4, 8), datetime(2016, 8, 18), datetime(2018, 9, 24), datetime(2019, 6, 10), datetime(2021, 8, 10)], test_date = datetime(2016, 6, 6)

Output : 2016-08-18 00:00:00

Explanation : Nearest date to 6 June 2016 is 18 August 2016 in list.

Method #1 : Using min() + dictionary comprehension + abs()

In this, a difference of each date with a given date is computed as key and date as a value using abs(). The min(), is used to get the minimum key difference and its value is the result.

Python3




# Python3 code to demonstrate working of
# Nearest date in List
# Using min() + dictionary comprehension + abs()
from datetime import datetime
  
# initializing datelist
test_date_list = [datetime(2020, 4, 8), datetime(2016, 8, 18), 
                  datetime(2018, 9, 24), datetime(2019, 6, 10),
                  datetime(2021, 8, 10)]
               
# printing original list
print("The original list is : " + str(test_date_list))
  
# initializing test date 
test_date = datetime(2017, 6, 6)
  
# get all differences with date as values 
cloz_dict =
  abs(test_date.timestamp() - date.timestamp()) : date 
  for date in test_date_list}
  
# extracting minimum key using min()
res = cloz_dict[min(cloz_dict.keys())]
  
# printing result
print("Nearest date from list : " + str(res))

Output:

The original list is : [datetime.datetime(2020, 4, 8, 0, 0), datetime.datetime(2016, 8, 18, 0, 0), datetime.datetime(2018, 9, 24, 0, 0), datetime.datetime(2019, 6, 10, 0, 0), datetime.datetime(2021, 8, 10, 0, 0)]

Nearest date from list : 2016-08-18 00:00:00

Method #2 : Using min() + abs() + lambda

Another simpler way to solve this problem is to get differences and capture the minimum one using min(). This is a flexible method and can be extended to every datatype not only datetime. 

Python3




# Python3 code to demonstrate working of
# Nearest date in List
# Using min() + abs() + lambda
from datetime import datetime
  
# initializing datelist
test_date_list = [datetime(2020, 4, 8), datetime(2016, 8, 18), 
                  datetime(2018, 9, 24), datetime(2019, 6, 10),
                  datetime(2021, 8, 10)]
               
# printing original list
print("The original list is : " + str(test_date_list))
  
# initializing test date 
test_date = datetime(2017, 6, 6)
  
# shorthand using lambda function for compact solution
res = min(test_date_list, key=lambda sub: abs(sub - test_date))
  
# printing result
print("Nearest date from list : " + str(res))

Output:

The original list is : [datetime.datetime(2020, 4, 8, 0, 0), datetime.datetime(2016, 8, 18, 0, 0), 

datetime.datetime(2018, 9, 24, 0, 0), datetime.datetime(2019, 6, 10, 0, 0), datetime.datetime(2021, 8, 10, 0, 0)]

Nearest date from list : 2016-08-18 00:00:00

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :