
Getting the first and last item in a python for loop
By: a guest on Jan 28th, 2012 | syntax:
None | size: 0.79 KB | hits: 14 | expires: Never
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
from calendar import Calendar, SUNDAY
cal = Calendar(SUNDAY)
month_dates = list(cal.itermonthdates(year, month))
month_start = month_dates[0]
month_end = month_dates[-1]
for i, date in enumerate(month_dates):
if i == 0:
month_start = date
month_end = date
month_dates = cal.itermonthdates(year, month)
month_start = month_dates.next()
for month_end in month_dates: pass # bletcherous
month_dates = cal.itermonthdates(year, month)
month_start = month_dates.next()
for date in month_dates:
pass
month_end = date