Building a list of months by iterating between two dates in a list (Python)
<p>I have an ordered (i.e. sorted) list that contains dates sorted (as datetime objects) in ascending order.</p>

<p>I want to write a function that iterates through this list and generates another list of the first available dates for each month.</p>

<p>For example, suppose my sorted list contains the following data:</p>

<pre><code>A = [
'2001/01/01',
'2001/01/03',
'2001/01/05',
'2001/02/04',
'2001/02/05',
'2001/03/01',
'2001/03/02',
'2001/04/10',
'2001/04/11',
'2001/04/15',
'2001/05/07',
'2001/05/12',
'2001/07/01',
'2001/07/10',
'2002/03/01',
'2002/04/01',
]
</code></pre>

<p>The returned list would be </p>

<pre><code>B = [
'2001/01/01',
'2001/02/04',
'2001/03/01',
'2001/04/10',
'2001/05/07',
'2001/07/01',
'2002/03/01',
'2002/04/01',
]
</code></pre>

<p>The logic I propose would be something like this:</p>

<pre><code>def extract_month_first_dates(input_list, start_date, end_date):
    #note: start_date and end_date DEFINITELY exist in the passed in list
    prev_dates, output = [],[]  # &lt;- is this even legal?
    for (curr_date in input_list):
        if ((curr_date &lt; start_date) or (curr_date &gt; end_date)):
            continue

        curr_month = curr_date.date.month
        curr_year = curr_date.date.year
        date_key = "{0}-{1}".format(curr_year, curr_month)
        if (date_key in prev_dates):
            continue
        else:
            output.append(curr_date)
            prev_dates.append(date_key)

    return output
</code></pre>

<p>Any comments, suggestions? - can this be improved to be more 'Pythonic' ?</p>
