Sorting lists in dictionary based on other list without assigning them again

Issue

I have a large dictionary of which I want to sort the list values based on one list. For a simple dictionary I would do it like this:

d = {'a': [2, 3, 1], 'b': [103, 101, 102]}

d['a'], d['b'] = [list(i) for i in zip(*sorted(zip(d['a'], d['b'])))]

print(d)

Output:

{'a': [1, 2, 3], 'b': [102, 103, 101]}

My actual dictionary has many keys with list values, so unpacking the zip tuple like above becomes unpractical. Is there any way to do the go over the keys and values without specifying them all? Something like:

d.values() = [list(i) for i in zip(*sorted(zip(d.values())))]

Using d.values() results in SyntaxError: can't assign function call, but I’m looking for something like this.

Solution

If you have many keys (and they all have equal length list values) using pandas sort_values would be an efficient way of sorting:

d = {'a': [2, 3, 1], 'b': [103, 101, 102], 'c' : [4, 5, 6]}
d = pd.DataFrame(d).sort_values(by='a').to_dict('list')

Output:

{'a': [1, 2, 3], 'b': [102, 103, 101], 'c': [6, 4, 5]}

If memory is an issue, you can sort in place, however since that means sort_values returns None, you can no longer chain the operations:

df = pd.DataFrame(d)
df.sort_values(by='a', inplace=True)
d = df.to_dict('list')

The output is the same as above.

Answered By – Nick

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published