Python | Sort list of dates given as strings Last Updated : 31 Dec, 2024 Comments Improve Suggest changes Like Article Like Report To sort a list of dates given as strings in Python, we can convert the date strings to datetime objects for accurate comparison. Once converted, the list can be sorted using Python's built-in sorted() or list.sort() functions. This ensures the dates are sorted chronologically.Using pandas.to_datetime()This method converts date strings to datetime objects using pandas, which is efficient for handling large datasets and supports various date formats. We can then sort the dates using sort_values(). Python import pandas as pd # Example list of date strings dates = ["24 Jul 2017", "25 Jul 2017", "11 Jun 1996", "01 Jan 2019", "12 Aug 2005", "01 Jan 1997"] # Convert the list of dates into a pandas Series s1 = pd.Series(dates) # Convert the strings to datetime objects and sort s2 = s1.apply(pd.to_datetime, format='%d %b %Y').sort_values() # Convert sorted datetime objects back to the string format s3 = s2.dt.strftime('%d %b %Y').tolist() print(s3) Output['11 Jun 1996', '01 Jan 1997', '12 Aug 2005', '24 Jul 2017', '25 Jul 2017', '01 Jan 2019'] Let's understand other methods to sort list of dates:Using datetime.strptime()This method converts date strings into datetime objects using Python's datetime module. It allows for precise date comparison and can be used with sorted() to sort the list. Python from datetime import datetime # Example list of date strings dates = ["24 Jul 2017", "25 Jul 2017", "11 Jun 1996", "01 Jan 2019", "12 Aug 2005", "01 Jan 1997"] # Sorting using sorted() and converting string to datetime object s1 = sorted(dates, key=lambda x: datetime.strptime(x, '%d %b %Y')) # Converting datetime objects back to string format s2 = [datetime.strftime(datetime.strptime(d, '%d %b %Y'), '%d %b %Y') for d in s1] print(s2) Output['11 Jun 1996', '01 Jan 1997', '12 Aug 2005', '24 Jul 2017', '25 Jul 2017', '01 Jan 2019'] Using list.sort() This method sorts the list in place by converting date strings to datetime objects, making the sorting process efficient for smaller datasets when we don't need to preserve the original list. Python from datetime import datetime # Example list of date strings dates = ["24 Jul 2017", "25 Jul 2017", "11 Jun 1996", "01 Jan 2019", "12 Aug 2005", "01 Jan 1997"] # Sorting the list in place using list.sort() and datetime conversion dates.sort(key=lambda x: datetime.strptime(x, '%d %b %Y')) # Output the sorted list print(dates) Output['11 Jun 1996', '01 Jan 1997', '12 Aug 2005', '24 Jul 2017', '25 Jul 2017', '01 Jan 2019'] Comment More infoAdvertise with us Next Article Python | Sort list of dates given as strings R rituraj_jain Follow Improve Article Tags : Technical Scripter Python Python Programs Technical Scripter 2018 python-string Python string-programs +2 More Practice Tags : python Similar Reads How to sort a list of strings in Python In this article, we will explore various methods to sort a list of strings in Python. The simplest approach is by using sort().Using sort() MethodThe sort() method sorts a list in place and modifying the original list directly.Pythona = ["banana", "apple", "cherry"] # Sorting list in place a.sort() 2 min read Python | Sort all sublists in given list of strings Sorting sublists in a list of strings refers to arranging the elements within each sublist in a specific order. There are multiple ways to sort each list in alphabetical order, let's understand each one by one.Using list comprehensionList comprehension with sorted() allows efficient sorting of each 2 min read Sort Numeric Strings in a List - Python We are given a list of numeric strings and our task is to sort the list based on their numeric values rather than their lexicographical order. For example, if we have: a = ["10", "2", "30", "4"] then the expected output should be: ["2", "4", "10", "30"] because numerically, 2 < 4 < 10 < 30. 2 min read Python - Sort by Rear Character in Strings List Given a String list, perform sort by the rear character in the Strings list. Input : test_list = ['gfg', 'is', 'for', 'geeks'] Output : ['gfg', 'for', 'is', 'geeks'] Explanation : g < r < s = s, hence the order. Input : test_list = ['gfz', 'is', 'for', 'geeks'] Output : ['for', 'is', 'geeks', 5 min read Python | Sort given list of dictionaries by date Given a list of dictionary, the task is to sort the dictionary by date. Let's see a few methods to solve the task. Method #1: Using naive approach Python3 # Python code to demonstrate # sort a list of dictionary # where value date is in string # Initialising list of dictionary ini_list = [{'name':'a 2 min read Python | Sort given list of strings by part of string We are given with a list of strings, the task is to sort the list by part of the string which is separated by some character. In this scenario, we are considering the string to be separated by space, which means it has to be sorted by second part of each string. Using sort() with lambda functionThis 3 min read Python | Sort each String in String list Sometimes, while working with Python, we can have a problem in which we need to perform the sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let's discuss certain ways in which this problem can be solved. Method #1 : Usi 4 min read Python - Sort List items on basis of their Digits Given List of elements, perform sorting on basis of digits of numbers. Input : test_list = [434, 211, 12, 3] Output : [12, 211, 3, 434] Explanation : 3 < 12, still later in list, as initial digit, 1 < 3. Hence sorted by digits rather than number. Input : test_list = [534, 211, 12, 7] Output : 2 min read Custom Sorting in List of Tuples - Python The task of custom sorting in a list of tuples often involves sorting based on multiple criteria. A common example is sorting by the first element in descending order and the second element in ascending order. For example, given a = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)], sorting the tuples by t 3 min read Creating a list of range of dates in Python Given a date, the task is to write a Python program to create a list of a range of dates with the next k dates starting from the current date. For example, if the given date is 4th January 1997 and k is 5, the output should be a list containing 4th January 1997, 5th January 1997, 6th January 1997, 7 3 min read Like