Reputation: 35
I have a list in the form of:
months = ['August', 'September', 'October', 'November', 'December', 'January']
I need the output for the months to be arranged in the form of calendar year as below:
months : [January, 'August', 'September', 'October', 'November', 'December']
.
I have been trying to define my custom sort function as shown below:
m = {"January":0, "February":1, "March":2, "April":3, "May":4, "June":5, "July":6, "August":7, "September":8, "October":9, "November":10, "December":11}
sorted(months,key=m)
But I am getting error as dict
object is not callable.
I am not sure if this approach is right and how should I proceed in this case.
Upvotes: 0
Views: 110
Reputation: 611
months = ['August', 'September', 'October', 'November', 'December', 'January']
order = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
print(sorted(months,key=order.index))
Upvotes: 0
Reputation: 3624
key takes a function as value that is why you are getting the error. Try this:
sorted(months,key=lambda x: m[x])
Upvotes: 1