Python - Find the closest date from a List - GeeksforGeeks

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.
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 datelisttest_date_list = [datetime(2020, 4, 8), datetime(2016, 8, 18),                   datetime(2018, 9, 24), datetime(2019, 6, 10),                  datetime(2021, 8, 10)]               # printing original listprint("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 resultprint("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
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() + lambdafrom datetime import datetime  # initializing datelisttest_date_list = [datetime(2020, 4, 8), datetime(2016, 8, 18),                   datetime(2018, 9, 24), datetime(2019, 6, 10),                  datetime(2021, 8, 10)]               # printing original listprint("The original list is : " + str(test_date_list))  # initializing test date test_date = datetime(2017, 6, 6)  # shorthand using lambda function for compact solutionres = min(test_date_list, key=lambda sub: abs(sub - test_date))  # printing resultprint("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
Python datetime-program
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?