0

I have a python dictionary with the following format.

{'range_qty': 
  {'0 to 10 qty': 5,
  'more than 5000 qty': 18,
  '500 to 1000 qty': 20,
  '200 to 500 qty': 19,
  '1000 to 5000 qty': 15,
  '10 to 50 qty': 3,
  '50 to 200 qty': 14}}

How can I sort this dictionary by the key ? I need output like

{'range_qty': 
  {'0 to 10 qty': 5,
  '10 to 50 qty': 3,
  '50 to 200 qty': 14,
  '200 to 500 qty': 19,
  '500 to 1000 qty': 20,
  '1000 to 5000 qty': 15,
  'more than 5000 qty': 18,
  }}
M Usman Wahab
  • 53
  • 1
  • 10
  • 1
    Have you tried anything already to solve this? – Thierry Lathuille May 06 '20 at 08:54
  • 1
    I'm very curious - why are you trying to sort a dictionary at all? – Mark Snyder May 06 '20 at 08:55
  • 1
    [This](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) is literally the **first** link you find if you search the internet for 'python sort dict' – mapf May 06 '20 at 08:56
  • Just a heads up, all these "values" are strings, and will not nessecarily be sorted according to values or sizes. If you want to sort by quantity, convert these into actual values such as `0`, `10`, `50` and so on, and measure the gap from the last key to the current key to determinate the quantity in between. – Torxed May 06 '20 at 08:58
  • @mapf I'm inclined to agree with your link, the problem is that OP's values aren't corresponding to the key text. For instance, `50 to 200 qty` has a value of `14` which is no where in between. OP also wants to sort it by the key, not the value, although that change in the linked answer is minuscule. – Torxed May 06 '20 at 08:59
  • Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – RMPR May 06 '20 at 09:00
  • There are other difficulties here: the keys can't be sorted alphabetically, and they don't even have the same structure. That's probably not a very useful choice of keys... – Thierry Lathuille May 06 '20 at 09:00
  • @RMPR I tried every soln provided by your link but couldn't work for me. – M Usman Wahab May 06 '20 at 09:06

2 Answers2

1

Using a custom sort.

Ex:

import sys


def cust_sort(val):
    i = val[0].split(" ", 1)[0]
    if not i.isdigit():
        return sys.maxsize
    return int(i)

data = {'range_qty': 
  {'0 to 10 qty': 5,
  'more than 5000 qty': 18,
  '500 to 1000 qty': 20,
  '200 to 500 qty': 19,
  '1000 to 5000 qty': 15,
  '10 to 50 qty': 3,
  '50 to 200 qty': 14}}

data = sorted(data['range_qty'].items(), key=cust_sort)
#or data = {'range_qty': dict(sorted(data['range_qty'].items(), key=cust_sort))}
print(data)

Output:

[('0 to 10 qty', 5),
 ('10 to 50 qty', 3),
 ('50 to 200 qty', 14),
 ('200 to 500 qty', 19),
 ('500 to 1000 qty', 20),
 ('1000 to 5000 qty', 15),
 ('more than 5000 qty', 18)]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Depending on your python version your dict by default might not store order. However if you wanted to iterate over this dict in the order you listed you can use sort if you make your keys uniform. I.E make them all start with an int. So instead of more than 5000 qty you could change it to 5000 or more qty

data = {'range_qty':
  {'0 to 10 qty': 5,
  '5000 or more qty': 18,
  '500 to 1000 qty': 20,
  '200 to 500 qty': 19,
  '1000 to 5000 qty': 15,
  '10 to 50 qty': 3,
  '50 to 200 qty': 14}}

for qty in sorted(data['range_qty'], key=lambda text: int(text.split()[0])):
    print(f"{qty}: {data['range_qty'][qty]}")

OUTPUT

0 to 10 qty: 5
10 to 50 qty: 3
50 to 200 qty: 14
200 to 500 qty: 19
500 to 1000 qty: 20
1000 to 5000 qty: 15
5000 or more qty: 18
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42