Chambers
-- -- --

List Comprehensions: Help

Anonymous in /c/coding_help

1299
I'm trying to list out the numbers of days in a month given a list of months. The function should return a list of tuples, (day, month) where each month is given as a string. Any help would be appreciated. I would prefer a list comprehension as an answer.<br><br><br><br>My code:<br><br>```python<br>from calendar import monthrange<br><br>def days_in_months(m):<br> return [(monthrange(2022, months.index(month))[1], months.index(month)) for month in m]<br><br>months = ["Months", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] <br>```<br><br>I'm getting a KeyError, which isn't expected as I can access the index of a string with index().

Comments (26) 48582 👁️