![]() |
'dict_items' object has no attribute 'sort' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: 'dict_items' object has no attribute 'sort' (/thread-37857.html) |
'dict_items' object has no attribute 'sort' - Calli - Jul-29-2022 monthdays = {'Jan':31, 'Feb':28, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':31, 'Sep':30, 'Oct':31, 'Nov':30, 'Dec':31} months = monthdays.items() months.sort(lambda f, s: cmp(f[1], s[1])) for month, days in months: print("There are ",days," days in a ", month)Error Objective get a list of tuple pairs using the items method
RE: 'dict_items' object has no attribute 'sort' - Gribouillis - Jul-29-2022 Use months = list(monthdays.items()) RE: 'dict_items' object has no attribute 'sort' - Axel_Erfurt - Jul-29-2022 I don't know what you want to do, but months are always sorted monthdays = {'Jan':31, 'Feb':28, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':31, 'Sep':30, 'Oct':31, 'Nov':30, 'Dec':31} for month in monthdays: print(f"There are {monthdays[month]} days in {month}")
RE: 'dict_items' object has no attribute 'sort' - Calli - Jul-29-2022 (Jul-29-2022, 09:10 PM)Gribouillis Wrote: Use It's saying
RE: 'dict_items' object has no attribute 'sort' - Gribouillis - Jul-29-2022 (Jul-29-2022, 09:12 PM)Calli Wrote: It's saying Error:sort() takes no positional argumentsUse months = sorted(monthdays.items(), key=lambda p: p[1]) RE: 'dict_items' object has no attribute 'sort' - Calli - Jul-29-2022 (Jul-29-2022, 09:15 PM)Gribouillis Wrote:(Jul-29-2022, 09:12 PM)Calli Wrote: It's saying Error:sort() takes no positional argumentsUse Thank you so much works like charm now RE: 'dict_items' object has no attribute 'sort' - Gribouillis - Jul-29-2022 (Jul-29-2022, 09:17 PM)Calli Wrote: Thank you so much works like charm nowA slightly more educated version: from operator import itemgetter months = sorted(monthdays.items(), key=itemgetter(1)) |