3

I have tried using this but the output is not as desired

m = []
import calendar
for i in range(1, 13):
    m.append(calendar.month_name)
print(m)

Output: [<calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>]

Shilpi Tiwari
  • 53
  • 1
  • 1
  • 4
  • 1
    you are not using 'i' within the range block.. so whatever is being appended to m is identical each time the loop iterates – Anita W Mar 24 '21 at 22:41
  • You probably just want something like `[calendar.month_name[i] for i in range(1, 13)]` – juanpa.arrivillaga Mar 24 '21 at 22:43
  • "but the output is not as desired" When you ask here, you should say explicitly what the desired output is. This time we were able to guess; it won't always be obvious. – Karl Knechtel Mar 24 '21 at 22:44
  • You appended the entire `month_name` object 12 times, without ever referencing an individual month. You need to refer to the package documentation and follow the usage examples and specifications you find there. Stack Overflow is not intended to replace existing documentation and tutorials. – Prune Mar 24 '21 at 22:49
  • Why even make a new list from `calendar.month_name`? Just use it directly by subscripting it. With your method, you will need to covert every month such a `1` for January to `0` for your list `m`. Are you sure you want to do this? – dawg Mar 24 '21 at 22:58

4 Answers4

14

The month_name element acts like a list.

You can either subscript it:

>>> calendar.month_name[3]
'March'

Or use list on it:

>>> import calendar
>>> list(calendar.month_name)
['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

Note the blank at index 0. There is no month zero...

Which leads to the other issue in your code. If you correct your code to be:

import calendar
m=[calendar.month_name[i] for i in range(1,12)]
# or
m=calendar.month_name[1:]

In either case you now have turned 'January' into element 0 instead of element 1. You will need to covert every date.

dawg
  • 98,345
  • 23
  • 131
  • 206
  • 2
    It's not really a list, that is probably the main point of confusion in the OP here. It's a `calendar._localized_month` object, which can be converted to one. – Jan Christoph Terasa Mar 24 '21 at 22:49
1

It outputs an array, so simply convert all to list list(calendar.month_name[1:]) - and you have the list of names.

crusher083
  • 171
  • 2
  • 10
0

Read the docs, then do as they say, and just index the array:

m = []
import calendar
for i in range(1, 13):
    m.append(calendar.month_name[i]) # month_name is an array
print(m)

This can be done easier using slicing (index 0 contains an empty string, as per the docs):

import calendar
m = calendar.month_name[1:]
print(m)
Jan Christoph Terasa
  • 5,781
  • 24
  • 34
0

Another way to do it is as follows:

m = []
import calendar
for month in calendar.month_name:
    m.append(month)

m.pop(0) #remove the empty value
print(m)