Search code examples
pythonglob

Filter a list in order to get the last day (in the list) for each month


I am using the following code to search through a specific path:

path = glob.glob(C:/*/202*, recursive=True)

And I get the following list of strings as output:

C:/some_path\\20200102
C:/some_path\\20200131
C:/some_path\\20200228
C:/some_path\\20200310
C:/some_path\\20200331

The final folder means YYYYMMdd (Year-Month-day)

And I want to filter this list of strings in order to get only the biggest day (the day number) of the month in the list.

Output desired:

C:/some_path\\20200131
C:/some_path\\20200228
C:/some_path\\20200331

I tried doing:

for i in path:
    filtered = max(path)

But this only retrieves the last date of the list. Not for each month as I want.


Solution

    1. group by month
    2. sort each group
    3. print last one
    from itertools import groupby
    
    path = ['C:/some_path\\20200102',
    'C:/some_path\\20200131',
    'C:/some_path\\20200111',
    'C:/some_path\\20200228',
    'C:/some_path\\20200310',
    'C:/some_path\\20200331']
    
    path_by_month = groupby(path, key=lambda x: x[:-2])
    for k, g in path_by_month:
        g = sorted(g)
        print(g[-1])