How to sort my dictionary using this condition

Viewed 822

How to sort my dictionary using this condition in python.

I have a Nested Dictionary which is describe below.

my_dict={
         "Moli": {"Price": 226, "Quantity": 300, "PreOrder": "Yes", "PostOrder": "No", "Dif": 36},
         "Anna": {"Price": 530, "Quantity": 154, "PreOrder": "No", "PostOrder": "Yes", "Dif": 26},
         "Bob": {"Price": 250, "Quantity": 100, "PreOrder": "No", "PostOrder": "No", "Dif": 46},
         "Annie": {"Price": 656, "Quantity": 96, "PreOrder": "Yes", "PostOrder": "No", "Dif": 55},
         "Calley": {"Price": 546, "Quantity": 76, "PreOrder": "No", "PostOrder": "Yes", "Dif": 35}
        }

I want to apply a condition on this nested dictionary. The condition is_

Sort the items where ("PreOrder"== "Yes" or "PostOrder"== "Yes") and show those item in descending order using "Quantity" value

Something like this.

my_dict2 = {("PreOrder"== "Yes" or "PostOrder"== "Yes") i[0]: i[1] for i in sorted(my_dict.items(), key=lambda x: x[1]["Quantity"], reverse=True)}

I know it's a wrong code.

expected output_

my_dict2={
         "Moli": {"Price": 226, "Quantity": 300, "PreOrder": "Yes", "PostOrder": "No", "Dif": 36},
         "Anna": {"Price": 530, "Quantity": 154, "PreOrder": "No", "PostOrder": "Yes", "Dif": 26},
         "Annie": {"Price": 656, "Quantity": 96, "PreOrder": "Yes", "PostOrder": "No", "Dif": 55},
         "Calley": {"Price": 546, "Quantity": 76, "PreOrder": "No", "PostOrder": "Yes", "Dif": 35}
         }

I would be very grateful if you could help me

2 Answers

This involves multiple steps as you describe. First, we need a dict with only the items that we want. This filters the dict.

filtered = {k: v for k, v in my_dict.items() if v['PreOrder'] == 'Yes' or v['PostOrder'] == 'Yes'}

Then you want to show the items in order. This implies a list, not a dictionary. So we'll make one with tuples from the original dict.

items = [(k, v) for k, v in filtered.items()]

Finally, we need to sort the items. As you've written already, sort accepts any arbitrary sorting method, so we can pass in a lambda function for that.

ordered = sorted(items, key=lambda i: i[1]['Quantity'], reversed=True)

In general, it is very helpful to think about your problems in minimal steps.

You can also do this using pandas library:

This saves a lot of lines of code and also reduces a lot of complexity.

Install pandas:

pip install pandas

Then, create a dataframe from my_dict:

In [229]: import pandas as pd

In [230]: df = pd.DataFrame(my_dict)

In [235]: df1 = df.T

In [244]: my_dict2 = df1[df1.PreOrder.eq('Yes') | df1.PostOrder.eq('Yes')].sort_values(by='Quantity', ascending=False).T.to_dict()

In [245]: my_dict2
Out[245]: 
{'Moli': {'Price': 226,
  'Quantity': 300,
  'PreOrder': 'Yes',
  'PostOrder': 'No',
  'Dif': 36},
 'Anna': {'Price': 530,
  'Quantity': 154,
  'PreOrder': 'No',
  'PostOrder': 'Yes',
  'Dif': 26},
 'Annie': {'Price': 656,
  'Quantity': 96,
  'PreOrder': 'Yes',
  'PostOrder': 'No',
  'Dif': 55},
 'Calley': {'Price': 546,
  'Quantity': 76,
  'PreOrder': 'No',
  'PostOrder': 'Yes',
  'Dif': 35}}
Related