dict_items object has no attribute 'sort'

50,688

Solution 1

Haven't tested but a theory: you are using python3!

From https://docs.python.org/3/whatsnew/3.0.html

dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. For example, this no longer works: k = d.keys(); k.sort(). Use k = sorted(d) instead (this works in Python 2.5 too and is just as efficient).

as I understand it a "view" is an iterator, and an iterator does not have the sort function. Change it to

sorted(all_colors)

according to the documentation

Solution 2

So the total solution based on Johan's answer is:

all_colors = sorted(reportlab.lib.colors.getAllNamedColors().items())

Solution 3

I believe the sort() method doesn't support Python 3.x anymore.

It is necessary to pass the corresponding variable to the sorted(all_colors).

Share:
50,688
Imran Qadir Baksh - Baloch
Author by

Imran Qadir Baksh - Baloch

Updated on October 03, 2020

Comments