Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort dictionary of dictionaries by value

Tags:

I have this dictionary:

statuses = {             'pending' : {'status_for':'all', 'position':1},             'cancelled' : {'status_for':'all','position':2},             'approved' : {'status_for':'owner', 'position':1},             'rejected - owner' : {'status_for':'owner', 'position':2},             'accepted' : {'status_for':'dev', 'position':1},             'rejected - developer' : {'status_for':'dev', 'position':3},             'closed' : {'status_for':'dev', 'position':5},             } 

I've also got a function to pull all status_for values of either owner or dev that looks like this and put it into a PyQt QComboBox:

for s in statuses:             if statuses[s]['status_for'] == "dev" or statuses[s]['status_for'] == "all":                 cb_developer_status.addItem(s.capitalize(), s)    

I'd like to order these by the position value though. What is a good way to do this, so that when I populate by combobox I have it in a predefined order?

I realize that the snippet above is checking for both 'dev' and 'all', my assumption right now is that I'd have to loop through the dictionary twice to get the two separate blocks in the order I wish (ie. 'all' appears before 'dev').

I saw this post, but I'm not sure how to convert this answer to something that is a dictionary of dictionaries.

like image 589
Andy Avatar asked Nov 26 '12 21:11

Andy


People also ask

How do you sort dictionaries by dictionary value?

To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function.

Can you sort dictionaries by value in Python?

To sort a dictionary by value in Python you can use the sorted() function. Python's sorted() function can be used to sort dictionaries by key, which allows for a custom sorting method. sorted() takes three arguments: object, key, and reverse. Dictionaries are unordered data structures.

Can you sort values in a dictionary?

It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list—probably a list of tuples.

How do you sort nested dictionary by value?

Method #1 : Using OrderedDict() + sorted() This task can be performed using OrderedDict function which converts the dictionary to specific order as mentioned in its arguments manipulated by sorted function in it to sort by the value of key passed.


1 Answers

Would something like this work? Similar to the post you linked, this uses the key function of sorted to provide a custom sort order. iteritems() returns a (key, value) tuple, so that gets passed into lambda (x, y): y['position'], where y['position'] is the value (your nested dictionary, keyed by the status), and position is the item by which you want to sort.

In [35]: statuses = {             'pending' : {'status_for':'all', 'position':1},             'cancelled' : {'status_for':'all','position':2},             'approved' : {'status_for':'owner', 'position':1},             'rejected - owner' : {'status_for':'owner', 'position':2},             'accepted' : {'status_for':'dev', 'position':1},             'rejected - developer' : {'status_for':'dev', 'position':3},             'closed' : {'status_for':'dev', 'position':5},             }  In [44]: for s in sorted(statuses.iteritems(), key=lambda (x, y): y['position']):    ....:     print s    ....:    ....: ('accepted', {'position': 1, 'status_for': 'dev'}) ('approved', {'position': 1, 'status_for': 'owner'}) ('pending', {'position': 1, 'status_for': 'all'}) ('rejected - owner', {'position': 2, 'status_for': 'owner'}) ('cancelled', {'position': 2, 'status_for': 'all'}) ('rejected - developer', {'position': 3, 'status_for': 'dev'}) ('closed', {'position': 5, 'status_for': 'dev'}) 
like image 131
RocketDonkey Avatar answered Sep 29 '22 11:09

RocketDonkey