Python - Apply grouping on a list of string dates

  • Suppose we are having a list of dates, and we need to find how many times each date gets repeated. From database side, we normally achieve this using a group by clause. We will try the same in python without using any other third party libraries.

    groupDates() method in the below sample has the logic for the group by implementation.

    Loop through each date, and using List Comprehension, check if the date is already there. If so increase the count.
    Final result is a list of unique dates and the count of each date.
    Convert datetime to string dates, if needed.

    Copied
    from datetime import datetime
    
    def groupDates(dtlist):
        groupedList = []
        for dt in dtlist:
            if[gl[0] for gl in groupedList if gl[0] == dt]:
                for item in groupedList:
                    if(item[0] == dt):
                        item[1] = int(item[1]) + 1
            else:
                groupedList.append([dt, 1])
        
        return groupedList
    
    #Date in string format
    datelist = ['2020/10/15', '2020/10/15', '2021/04/10', '2022/02/05', '2022/02/05']
    #Convert to datetime objects
    dtlist = [datetime.strptime(dt, '%Y/%m/%d') for dt in datelist]
    
    #Call groupDates fuuunction to find the count
    groupedList = groupDates(dtlist)
    #Result contains datetime objects. Convert to string, if needed.
    stringdates = [ [datetime.strftime(dt[0], '%Y/%m/%d'), dt[1] ] for dt in groupedList]
    
    print(stringdates)
    
    Output:
    [
    	['2020/10/15', 2],
    	['2021/04/10', 1],
    	['2022/02/05', 2]
    ]
    
  • In the above example, input dates are in same format.
    If you have a list of dates that are in different formats, strptime() can't be used directly.
    Check the below sample for doing the conversion. Use the function formatDates() from this sample.
    Filter a list of string dates in different formats

Absolute Code Works - Python Topics