OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I sort a dictionary with in the dictionary by the keys of the second dictionary in python? [duplicate]

  • Thread starter Thread starter MohammadMahdi Saeedi
  • Start date Start date
M

MohammadMahdi Saeedi

Guest
I have a dictionary as follows:

Code:
dict_1 ={'Spain':{'wins':1,'loses':0,'draws':2,'goal_difference':2, 'points':3},'Portugal':{'wins':3,'loses:1,'draws':1,'goal_difference':0,'points':4},'Morocco':{'wins':2,'loses':2,'draws':0,'goal_difference':-2,'points':3}}

I want to sort this dictionary first by the value of points then if the points are equal I want to sort this dictionary by the value wins and lastly if the wins are also equal I want to sort this based on the order of alphabets in keys. So the end result is something like this:

Code:
dict_1 ={'Portugal':{'wins':3,'loses:1,'draws':1,'goal_difference':0,'points':4},'Morocco':{'wins':2,'loses':2,'draws':0,'goal_difference':-2,'points':3}},'Spain':{'wins':1,'loses':0,'draws':2,'goal_difference':2, 'points':3}

How can I do this?

I know If I want to sort a dictionary based on the values I can use lambda and sorted:

Code:
sorted(dict_1.items(), key=lambda x:x[1])

but I don't know how can I do this when I have dictionary with in the dictionary. I want to do this by lambda and sorted if possible.
<p>I have a dictionary as follows:</p>
<pre><code>dict_1 ={'Spain':{'wins':1,'loses':0,'draws':2,'goal_difference':2, 'points':3},'Portugal':{'wins':3,'loses:1,'draws':1,'goal_difference':0,'points':4},'Morocco':{'wins':2,'loses':2,'draws':0,'goal_difference':-2,'points':3}}
</code></pre>
<p>I want to sort this dictionary first by the value of <strong>points</strong> then if the points are equal I want to sort this dictionary by the value <strong>wins</strong> and lastly if the wins are also equal I want to sort this based on the order of alphabets in keys. So the end result is something like this:</p>
<pre><code>dict_1 ={'Portugal':{'wins':3,'loses:1,'draws':1,'goal_difference':0,'points':4},'Morocco':{'wins':2,'loses':2,'draws':0,'goal_difference':-2,'points':3}},'Spain':{'wins':1,'loses':0,'draws':2,'goal_difference':2, 'points':3}
</code></pre>
<p>How can I do this?</p>
<p>I know If I want to sort a dictionary based on the values I can use lambda and sorted:</p>
<pre><code>sorted(dict_1.items(), key=lambda x:x[1])
</code></pre>
<p>but I don't know how can I do this when I have dictionary with in the dictionary. I want to do this by lambda and sorted if possible.</p>
 

Latest posts

Top