Issues with Updating Script from Python 2 to Python 3

As the title says, I’ve been attempting to update this script I’m using from Python 2 to Python 3, but keep running into an issue with keys.
I’m running into an error message stating ‘dict_keys’ object has no attribute ‘sort’ whenever it attempts to run Line 192 in the code. Line 191 and 192 state:

Line 191: shapes = CONTROL_SHAPES.keys()
Line 192: shapes.sort()

I have attempted to fix with by by changing 191 to:

shapes = sorted(CONTROL_SHAPES.keys())


shapes = list(CONTROL_SHAPES.keys())

However neither have worked.
I’ve also attempted to rewrite Line 192 as:




However, neither of those have worked either and I was wondering if anyone on here knows what’s going on here and if anyone might have any suggestions as to what I could to to fix the issue.

You haven’t given a sample of what CONTROL_SHAPES dictionary looks like, and what is the desired outcome.

I am assuming you have a dictionary and want to sort it based on the keys.


# Original Dictionary
{'a': 44, 'y': 33, 'z': 11, 'b': 66, 'd': 11, 'h': 100, 'n': 331}

# Desired Dictionary
{'a': 44, 'b': 66, 'd': 11, 'h': 100, 'n': 331, 'y': 33, 'z': 11}

then the following should work:

shapes = dict(sorted(CONTROL_SHAPES.items()))

Alternatively, using a comprehension (though I prefer the above):

shapes = {key: CONTROL_SHAPES[key] for key in sorted(CONTROL_SHAPES)}

If you want to do something a bit differently (such as sorting based on value), then share an example of the original dictionary and the desired outcome dictionary.