In this tutorial, we will guide you through the process of sorting a list of date strings in Python. Specifically, we'll demonstrate how to sort a list by date and time, providing you with a clear example.
This Python program focuses on arranging a list of date strings in ascending order.
To begin, the program imports the datetime
module from the datetime
library, which is essential for handling date and time-related operations.
Next, it defines a list called inputDateList
, containing four date strings, each in the 'dd-mm-yyyy' format.
The program proceeds to utilize the sort()
function on the inputDateList
. During the sorting process, a lambda function is employed as the key
parameter to specify the comparison rule. The lambda function takes each date from the list and transforms it into a datetime
object using the strptime()
function.
The strptime()
function parses the provided date string according to the specified format ("%d-%m-%Y") and converts it into a datetime
object, facilitating proper sorting based on date and time.
Through this example, you will gain a comprehensive understanding of how to effectively sort a list of date and time values in Python.
main.py
# importing datetime
from datetime import datetime
# input list of date strings
inputDateList = ['23-07-2023', '05-06-2023', '04-06-2023', '02-06-2023']
# sorting the input list by formatting each date using the strptime() function
inputDateList.sort(key=lambda date: datetime.strptime(date, "%d-%m-%Y"))
# Printing the input list after sorting
print("List Sorting by Date:\n", inputDateList)
Output:
List Sorting by Date: ['02-06-2023', '04-06-2023', '05-06-2023', '23-06-2023']
Description:
- We import the
datetime
module from thedatetime
library. - We define a list called
inputDateList
containing four date strings in the 'dd-mm-yyyy' format. - We use the
sort()
method oninputDateList
, and we provide a lambda function as thekey
parameter. This lambda function converts each date string into adatetime
object using thestrptime()
function. - The
strptime()
function parses each date string according to the specified format ("%d-%m-%Y") and returns it as adatetime
object, allowing proper date sorting. - Finally, we print the sorted list, which will display the dates in ascending order
Here's another Python code example that demonstrates how to sort a list of date strings in ascending order using the datetime
module. In this example, we'll use a list of dates with various formats.
from datetime import datetime
# Define a list of date strings with different formats
inputDateList = ['10-08-2023', '22/05/21', '2022-12-05', 'Mar 15, 2020']
# Define a function to convert date strings into datetime objects
def convert_to_datetime(date_str):
formats = ["%d-%m-%Y", "%d/%m/%y", "%Y-%m-%d", "%b %d, %Y"]
for fmt in formats:
try:
return datetime.strptime(date_str, fmt)
except ValueError:
pass
# Sort the list by date
inputDateList.sort(key=convert_to_datetime)
# Print the sorted list
for date in inputDateList:
print(date)
In this example:
- We define a list called
inputDateList
containing date strings with different formats. - We create a function called
convert_to_datetime(date_str)
to convert date strings intodatetime
objects. This function iterates through a list of possible date formats until it successfully converts the string. This allows us to handle dates in various formats. - We use the
sort()
method oninputDateList
and provide theconvert_to_datetime
function as thekey
parameter. This function is responsible for converting each date string into adatetime
object for proper sorting. - The
strptime()
function is used within theconvert_to_datetime
function to parse each date string according to the specified formats. - Finally, we print the sorted list, which will display the dates in ascending order, regardless of their original format.
You might also like:
- Read Also: Laravel 10 Google Pie Chart Example
- Read Also: Python Data Types and Variables Example
- Read Also: How to Get Current Date and Time in Python
- Read Also: Python for Beginners: Getting Started with Python