title: Getting the first and last item in a python for loop
body: <p>Is there an elegant and pythonic way to trap the first and last item in a for loop which is iterating over a generator?</p>
  
  <pre><code>from calendar import Calendar
  
  cal = Calendar(6)
  month_dates = cal.itermonthdates(year, month)
  for date in month_dates:
      if (is first item):     # this is fake
          month_start = date
      if (is last item):      # so is this
          month_end = date
  </code></pre>
  
  <p>This code is attempting to get the first day of the week the month ends on, and the last day of the week the month ends on.  Example: for June, month-start should evaluate to 5/31/09.  Even though it's technically a day in May, it's the first day of the week that June begins on.</p>
  
  <p>Month-dates is a generator so i can't do the [:-1] thing. What's a better way to handle this?</p>