2

I created a dictionary using a for-loop and this code:

players[name] = {'roll_total': player_roll, 'ante': None}

Previously my dictionary was just players = {names: totals} and I could sort it using this code:

players = [(k, players[k]) for k in sorted(players, key=players.get, reverse=True)]

But now since I implemented the inner "attribute" dictionary, I get an error saying comparisons can't be made on dictionaries.

TypeError: '<' not supported between instances of 'dict' and 'dict'

So how can I modify the sorting method to compare values of the dictionaries (the roll_total values), and have my players dictionary sorted?

2
  • 1
    key=lambda x: players[x]['roll_total'] Commented Aug 18, 2018 at 19:07
  • Give a complete example of your current dict and your desired output. Commented Aug 18, 2018 at 19:07

2 Answers 2

2

That happens because you are comparing two dictionaries.Use:

players = [(k, players[k]) for k in sorted(players, key=lambda x: players[x]['roll_total'], reverse=True)]

The lambda function receives the key of name as x and then sorts on the basis of players[x]['roll_total'] which is basically players[name]['roll_total'].

Sign up to request clarification or add additional context in comments.

2 Comments

Why does your statement have (k, players[k]) as they key:value in this example statement for a dictionary comprehension? dict_variable = {key:value for (key,value) in dictonary.items()} Also, you have square brackets indicating a list instead of a dictionary, why?
Indeed it does because it creates a list of tuples. Why? Because that's what you have done in your question while sorting. If you want a dictionary of dictionaries instead you can modify the code : players = {{k: players[k]} for k in sorted(players, key=lambda x: players[x]['roll_total'], reverse=True)}
1

You can also make this a dictionary comprehension and then assign it to the variable name of the dictionary on which you're iterating. That way, you're retaining the dictionary and can change it at certain logical junctures, making it available in later program flow as sorted. For instance, maybe you want the winner to start the next round, or something like that, and instead of making a new dictionary from the list, you already have the dictionary at hand, in only one operation:

{k: players[k] for k in sorted(players, key=lambda x: players[x]['rolltotal'], reverse=True)}

2 Comments

Can you recommend me some reading, or code that I can run interactively, so I can understand this statement? dict_variable = {key:value for (key,value) in dictonary.items()} But in your statement, I don't see a (key,value) just a key, and then your statement doesn't have a dictionary.items().
@Casey One of the better ones IMO: compciv.org/guides/python/fundamentals/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.