Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to sort a Python date string list?
Python provides several methods to sort a list of date strings. We'll explore three approaches: using sort() with lambda functions, using sort() with custom functions, and using sorted() function.
Using sort() with Lambda Functions
The datetime.strptime() function converts date strings into datetime objects for proper chronological sorting:
# importing datetime
from datetime import datetime
# input list of date strings
date_list = ['06-2014', '08-2020', '4-2003', '04-2005', '10-2002', '12-2021']
# sorting the input list by formatting each date using the strptime() function
date_list.sort(key=lambda date: datetime.strptime(date, "%m-%Y"))
# Printing the input list after sorting
print("The input list of date strings after sorting:")
print(date_list)
The input list of date strings after sorting: ['10-2002', '4-2003', '04-2005', '06-2014', '08-2020', '12-2021']
Using sort() with Custom Function
Create a custom function that splits date strings and returns year and month for sorting:
# input list of date strings
date_list = ['06-2014', '08-2020', '4-2003', '04-2005', '10-2002', '12-2021']
# creating a function that accepts the input list as an argument
def sort_dates(date_string):
# splitting the list of elements based on the '-' separator
split_up = date_string.split('-')
# returning the year, the month of input list elements
# Here split_up[1] gives the year and split_up[0] gives month
return split_up[1], split_up[0]
# sorting the input list of date strings using the sort() function
# here the key is the function name
date_list.sort(key=sort_dates)
# Printing the input list after sorting
print("The input list of date strings after sorting:")
print(date_list)
The input list of date strings after sorting: ['10-2002', '4-2003', '04-2005', '06-2014', '08-2020', '12-2021']
Using sorted() Function
The sorted() function returns a new sorted list without modifying the original:
Syntax
sorted(iterable, key=key, reverse=reverse)
Parameters
iterable ? It is a sequence
key ? A function that determines the sorting order. Default is None
reverse ? Boolean value. False for ascending (default), True for descending
Example
date_list = ['06-2014', '08-2020', '4-2003', '04-2005', '10-2002', '12-2021']
def sort_dates(date_string):
# splitting the list of elements based on the '-' separator
split_up = date_string.split('-')
# returning the year, the month of input list elements
# Here split_up[1] gives the year and split_up[0] gives month
return split_up[1], split_up[0]
print("The input list of date strings after sorting:")
# sorting the input list of date strings using the sorted function
print(sorted(date_list, key=sort_dates))
The input list of date strings after sorting: ['10-2002', '4-2003', '04-2005', '06-2014', '08-2020', '12-2021']
Comparison
| Method | Modifies Original | Best For |
|---|---|---|
sort() with lambda |
Yes | Simple date formats |
sort() with function |
Yes | Complex sorting logic |
sorted() |
No | Preserving original list |
Conclusion
Use datetime.strptime() with lambda for robust date parsing. Use custom functions for complex date formats. Use sorted() when you need to preserve the original list.
