[Solved] when this code prints it has a 0. how do I get it to print without it?


month_name‘ “helpfully” pads the list with an empty string so that the list indices match up with the normal 1-based counting of the months. To work around this, I would slice the list and enumerate it with an explicit starting point of 1.

for i, m in enumerate(calendar.month_name[1:], 1):

Another, more cumbersome but possibly useful, method is to get an explicit iterator for the list and “pre-advance” it.

month_itr = iter(calendar.month_name)
next(month_itr)
for i, m in enumerate(month_itr, 1):

This might be more efficient than creating a new list, but depending on your use case the difference may be irrelevant.

Using itertools to do something similar:

from itertools import islice

# The explicit stop=None tells islice to go to the end of the iterable
for i, m in enumerate(islice(calendar.month_name, 1, None), 1):

0

solved when this code prints it has a 0. how do I get it to print without it?