# Python – Last business day of every month in year

• Difficulty Level : Easy
• Last Updated : 03 Aug, 2021

Given a year and weekday number, the task is to write a Python program to extract each date of the month corresponding to the weekday.

Input : year = 1997, weekdy = 5

Output : [’25/1/1997′, ’22/2/1997′, ’29/3/1997′, ’26/4/1997′, ’31/5/1997′, ’28/6/1997′, ’26/7/1997′, ’30/8/1997′, ’27/9/1997′, ’25/10/1997′, ’29/11/1997′, ’27/12/1997′]

Explanation : 5 is friday. Last weekday of january 1997 which is friday is 25.

Input : year = 1996, weekdy = 4

Output : [’26/1/1996′, ’23/2/1996′, ’29/3/1996′, ’26/4/1996′, ’31/5/1996′, ’28/6/1996′, ’26/7/1996′, ’30/8/1996′, ’27/9/1996′, ’25/10/1996′, ’29/11/1996′, ’27/12/1996′]

Explanation : 4 is thursday. Last weekday of january 1997 which is thursday is 26.

Method #1 : Using loop + max() + calendar.monthcalendar

In this, we perform the task of getting each month calendar using monthcalendar() from the calendar library. Each weekday date is extracted and the maximum of it, being the maximum of the whole month is the last weekday, hence extracted.

## Python3

 `# Python3 code to demonstrate working of``# Last weekday of every month in year``# Using loop + max() + calendar.monthcalendar``import` `calendar`` ` `# initializing year``year ``=` `1997`` ` `# printing Year``print``(``"The original year : "` `+` `str``(year))`` ` `# initializing weekday ``weekdy ``=` `5`` ` `# iterating for all months``res ``=` `[]``for` `month ``in` `range``(``1``, ``13``):``     ` `    ``# max gets last friday of each month of 1997``    ``res.append(``str``(``max``(week[weekdy] ``                       ``for` `week ``in` `calendar.monthcalendar(year, month))) ``+``               ``"/"` `+` `str``(month)``+` `"/"` `+` `str``(year))`` ` `# printing ``print``(``"Last weekdays of year : "` `+` `str``(res))`

Output:

The original year : 1997

Last weekdays of year : [’25/1/1997′, ’22/2/1997′, ’29/3/1997′, ’26/4/1997′, ’31/5/1997′, ’28/6/1997′, ’26/7/1997′, ’30/8/1997′, ’27/9/1997′, ’25/10/1997′, ’29/11/1997′, ’27/12/1997′]

Method #2: Using list comprehension

Similar to the above method, the only difference being the usage of list comprehension for a compact solution.

## Python3

 `# Python3 code to demonstrate working of``# Last weekday of every month in year``# Using list comprehension``import` `calendar`` ` `# initializing year``year ``=` `1997`` ` `# printing Year``print``(``"The original year : "` `+` `str``(year))`` ` `# initializing weekday ``weekdy ``=` `5`` ` `# list comprehension for shorthand``res ``=` `[``str``(``max``(week[weekdy]``               ``for` `week ``in` `calendar.monthcalendar(year, month))) ``+` `       ``"/"` `+` `str``(month)``+` `"/"` `+` `str``(year) ``for` `month ``in` `range``(``1``, ``13``)]`` ` `# printing ``print``(``"Last weekdays of year : "` `+` `str``(res))`

Output:

The original year : 1997

Last weekdays of year : [’25/1/1997′, ’22/2/1997′, ’29/3/1997′, ’26/4/1997′, ’31/5/1997′, ’28/6/1997′, ’26/7/1997′, ’30/8/1997′, ’27/9/1997′, ’25/10/1997′, ’29/11/1997′, ’27/12/1997′]

My Personal Notes arrow_drop_up