Generating Months Name as a List

A list is a collection data type that is ordered and mutable. Lists are one of the most versatile data structures in Python. We will use a Python list that will display all the names of the months using different approaches.

Using calendar Module

In this approach, we use the Python calendar module which provides us a direct way to access the month names. The calendar.month_name array stores the names of all the months. Then using the list() function, we convert this array into the list.

Python
import calendar

# list of month names using calendar module
list_of_months = list(calendar.month_name)[1:]

# Printing the month names as a list
print(list_of_months)

Output:

['January', 'February', 'March', 'April', 'May', 'June', 
'July', 'August', 'September', 'October', 'November', 'December']

Using datetime Module

In this example, we are going to use another popular Python module called, the datetime module. We will also use the list comprehension method to iterate over each month and store its value in the list. The strftime('%B') formats the date object to return the full month name.

Python
# importing datetime python module
import datetime

# Getting the list of month names
list_of_months = [datetime.date(2024, i, 1).strftime('%B') for i in range(1, 13)]

# printing the list of month names
print(list_of_months)

Output:

['January', 'February', 'March', 'April', 'May', 'June', 
'July', 'August', 'September', 'October', 'November', 'December']

Manual Creation of the List

The approach is simple and straightforward, just create a list of strings of month names manually, and print the month names list.

Python
# Create the list of month names manually
list_of_months = ['January', 'February', 'March', 'April', 
                  'May', 'June', 'July', 'August', 
                  'September', 'October', 'November', 'December']

# Printing the list of month names
print(list_of_months)

Output:

['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']

Using pandas Module

We can also use the pandas module, which is a popular tool used in data analysis tasks. We can use pandas date_range() function and provide a stat and end date as the range. The using the strftime() function, we can only extract the months name from it.

Python
# importing the pandas module
import pandas as pd

# Getting the list of month names
list_of_months = list(pd.date_range(start='2024-01-01', 
                                    periods=12, 
                                    freq='M').strftime('%B'))

# printing the list of month names
print(list_of_months)

Output:

['January', 'February', 'March', 'April', 'May', 'June', 
'July', 'August', 'September', 'October', 'November', 'December']

Note: The time complexity and auxiliary space in every case is O(1).


How to generate month names as list in Python?

Our task is to generate a Python list containing the names of all the months in the calendar from January to December, and our aim is to accomplish this efficiently. In this article, we will go through all possible approaches to achieve this task in Python.

Similar Reads

Generating Months Name as a List

A list is a collection data type that is ordered and mutable. Lists are one of the most versatile data structures in Python. We will use a Python list that will display all the names of the months using different approaches....

Contact Us