How to compare multiple dates in a list in Python?

python compare date to today
python compare date strings
python compare timestamps
python datetime compare date only
python compare dates
compare dates with time in python
comparing time in python
comparing datetime datetime objects python

I am wondering how am I able to compare dates in a list. I would like to extract the "earliest" date. (I did a for loop as I had to replace some characters with '-')

comment_list = comment_container.findAll("div", {"class" : "comment-date"})
D =[]

  for commentDate in comment_list:
    year, month, day = map(int, commentDate.split('-'))
    date_object = datetime(year, month, day)
    date_object = datetime.strptime(commentDate, '%Y-%m-%d').strftime('%Y-%m-%d')   
    D.append(date_object)

print(D)

Output:

['2018-06-26', '2018-04-01', '2018-07-19', '2018-04-23', '2018-08-25', '2018-06-08', '2018-06-14', '2018-07-08', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15']

I want to extract the earliest date:

Eg.

'2018-04-01'

Just use the min function:

A = ['2018-06-26', '2018-04-01', '2018-07-19', '2018-04-23', '2018-08-25', '2018-06-08', '2018-06-14', '2018-07-08', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15']
print(min(A))

produces

2018-04-01

Comparing dates in Python, This will sort all the dates which are available in the list. One can store the date class objects into the list using append() method. Comparing dates is quite easy in Python. Dates can be easily compared using comparison operators (like , >, =, >=, != etc.). Let’s see how to compare dates with the help of datetime module using Python. Code #1 : Basic

comment_list = comment_container.findAll("div", {"class" : "comment-date"})
D =[]

  for commentDate in comment_list:
    year, month, day = map(int, commentDate.split('-'))
    date_object = datetime(year, month, day)  
    D.append(date_object)

print(min(D))

You should keep the dates as datetime objects and then use the min() builtin function to determine the earliest date

How to compare two dates with datetime in Python, date objects representing the dates of year , month , and day . Use the built-in comparison operators (e.g. < , > , == ) to compare them. Comparing dates and times is a very crucial requirement in any programming language. Python has a datetime library which has many inbuilt functions to use date and time. Interestingly date and time can also be compared like mathematical comparison between various numbers.

from datetime import datetime 

D = ['2018-06-26', '2018-04-01', '2018-07-19', '2018-04-23', '2018-08-25', '2018-06-08',
 '2018-06-14', '2018-07-08', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15']
D.sort()
print(D[0])

or this if you dont want change D

T = D[:]
T.sort()
print(T[0])

Comparing dates in Python, Comparing dates and times is a very crucial requirement in any programming language. Python has a datetime library which has many inbuilt� Python Exercise: Get a list of dates between two dates Last update on February 26 2020 08:09:19 (UTC/GMT +8 hours) Python Datetime: Exercise-50 with Solution.

As suggested by Siong you can use min(D). You can achieve the same like this:

comment_list = comment_container.findAll("div", {"class" : "comment-date"})
D = [datetime.strptime(commentDate, '%Y-%m-%d') for commentDate in comment_list]

print(min(D))

Working with datetime.datetime objects is usually preferable since the comparisons you make are not based on the formatting of the string. You can always convert to string later on:

min_date_str = min(D).strftime('%Y-%m-%d')

Date time objects in Python, The above list is by no means comprehensive. A good resource for looking up additional maks variables can be found here. Converting from a� A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

If you are sure that all dates are correctly padded with zeros (i.e. 01 for January not 1 and so on) then simple min or max will be enough. However I want to note that tuples of ints might be also sorted which might be of use if you encounter mixed padded and not padded dates, consider for example:

d = ['2018-7-1','2018-08-01']
print(min(d)) #prints 2018-08-01 i.e. later date
print(min(d,key=lambda x:tuple(int(i) for i in x.split('-')))) #prints 2018-7-1

This solution assumes data are not broken, i.e. all elements produced by .split('-') might be turned into ints.

Comparing the values of two lists in Python, Comparing the values of two lists in Python This is of course done in Python. Follow me Duration: 2:55 Posted: Oct 2, 2019 Python Iterate over multiple lists simultaneously Iterating over single lists, refers to using for loops for iteration over a single element of a single list at a particular step whereas in iterating over multiple lists simultaneously, we refer using for loops for iteration over a single element of multiple lists at a particular step.

Using If Function to Compare Dates of Two Cells, In this article, we will compare dates using the IF function in Excel 2016. IF function We have two lists named Date Column 1 and Date Column 2. We will � compare multiple lists in python. Ask Question Asked 4 years, 3 months ago. Active 4 months ago. Viewed 2k times 0. 2. if I have n number of lists like below

Oracle ANY: Comparing a Value with a List or Subquery, The Oracle ANY operator is used to compare a value to a list of values or result set returned by a subquery. The following illustrates the syntax of the ANY� The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical Method 3 : Using sum() + zip() + len() Using sum() + zip(), we can get sum of one of the list as summation of 1 if both the index in two lists have equal elements, and then compare that number with size of other list. This also requires first to

Java Comparable Comparator, Java Comparable Comparator tutorial shows how to compare objects in Java with All Spring Boot Python C# Java JavaScript Subscribe implemented frequently in the API by: String, Wrapper classes, Date, Calendar List; // Comparing list of objects by multiple object fields class Person { private String� A step-by-step Python code example that shows how to select Pandas DataFrame rows between two dates. Provided by Data Interview Questions, a mailing list for coding and data interview problems.

Comments
  • I’m typing this on my phone so can’t test it, but should be something like 'D.sort()[0]' to sort and get first element. Or even easier, just use max or min
  • Your loop seems totally pointless, by the way. You extract the integer parts from the string, then build a new date, then ignore that and build another new date that you immediately turn back into the same string it was parsed from.
  • If the dates are strings in YYYY-MM-DD format, just min(comment_list) should do the trick.