Sorting all the sub Dictionary using values of one of the sub dictionary
<p>I have data structure like the below and would like to sort the 
all the sub dictionaries to be sorted based on the values of the 'order' column.</p>

<p>Input:</p>

<pre><code>to_sort = [
('Fruits', 
    {
    'size': {1:[4, 2, 7,9]}, 
    'name': {1:['Orange', 'Apple', 'Kiwi', 'Mango']},
    'color': {1:['Orange', 'Red', 'Brown','Green']},
    'order': {1:[2, 1, 4,3]}
    }
)
]
</code></pre>

<p>output:</p>

<pre><code>[
('Fruits', 
    {
    'size': {1:[2, 4, 9, 7]}, 
    'name': {1:['Apple', 'Orange', 'Mango', 'Kiwi']},
    'color':{1:['Red', 'Orange', 'Green', 'Brown']},
    'order':{1:[1, 2, 3, 4]}
    }
)
]
</code></pre>

<p>I tried using the lambda</p>

<pre><code>sort = to_sort[1]
print(sorted(sort.items(), key=lambda i: i['order'].values()))
</code></pre>

<p>i am getting "tuple indices must be integers or slices, not str" error</p>

<h2> How to deal with what you've got</h2>

<p>Your existing data structure is a bit crazy, but here is how I would handle it (<strong>edit</strong> suppose the key for the color list was <code>123</code>):</p>

<pre><code>&gt;&gt;&gt; to_sort = [
... ('Fruits', 
...     {
...     'size': {1:[4, 2, 7,9]}, 
...     'name': {1:['Orange', 'Apple', 'Kiwi', 'Mango']},
...     'color': {123:['Orange', 'Red', 'Brown','Green']},
...     'order': {1:[2, 1, 4,3]}
...     }
... )
... ]
&gt;&gt;&gt; d = to_sort[0][1]
&gt;&gt;&gt; keys = list(d.keys())
&gt;&gt;&gt; idx = keys.index('order')
&gt;&gt;&gt; ordered_kv = zip(keys, zip(*sorted(zip(*(d[k][n] for k in keys for n in d[k])), key = lambda t:t[idx])))
&gt;&gt;&gt; sorted_dict = {k:{n:list(v) for n in d[k]} for k,v in ordered_kv}
&gt;&gt;&gt; from pprint import pprint
&gt;&gt;&gt; pprint(sorted_dict)
{'color': {123: ['Red', 'Orange', 'Green', 'Brown']},
 'name': {1: ['Apple', 'Orange', 'Mango', 'Kiwi']},
 'order': {1: [1, 2, 3, 4]},
 'size': {1: [2, 4, 9, 7]}}
</code></pre>

<p>Let's break this down: First, I made a canonical list of keys and find the index of <code>'order'</code>:</p>

<pre><code>&gt;&gt;&gt; keys = list(to_sort[0][1].keys())
&gt;&gt;&gt; idx = keys.index('order')
</code></pre>

<p>The next step is to <code>zip</code> the internal lists together into tuples where each of the items share the same relative position:</p>

<pre><code>&gt;&gt;&gt; list(zip(*(d[k][n] for k in keys for n in d[k])))
[(4, 2, 'Orange', 'Orange'), (2, 1, 'Red', 'Apple'), (7, 4, 'Brown', 'Kiwi'), (9, 3, 'Green', 'Mango')]
</code></pre>

<p>This can be sorted now according the the <code>idx</code> position and then "unzipped" (which really just means applying the <a href="http://stackoverflow.com/questions/5917522/unzipping-and-the-operator">zip-splat</a> combination again:</p>

<pre><code>&gt;&gt;&gt; list(zip(*sorted(zip(*(d[k][n] for k in keys for n in d[k])), key=lambda t:t[idx])))
[(2, 4, 9, 7), (1, 2, 3, 4), ('Red', 'Orange', 'Green', 'Brown'), ('Apple', 'Orange', 'Mango', 'Kiwi')]
</code></pre>

<p>And finally, you rebuild your crazy dictionary with a dictionary comprehension, making sure to zip up your ordered values with the original keys:</p>

<pre><code>&gt;&gt;&gt; ordered_kv = zip(keys, zip(*sorted(zip(*(d[k][n] for k in keys for n in d[k])), key = lambda t:t[idx])))
&gt;&gt;&gt; sorted_dict = {k:{n:list(v) for n in d[k]} for k,v in ordered_kv}
&gt;&gt;&gt; from pprint import pprint
&gt;&gt;&gt; pprint(sorted_dict)
{'color': {123: ['Red', 'Orange', 'Green', 'Brown']},
 'name': {1: ['Apple', 'Orange', 'Mango', 'Kiwi']},
 'order': {1: [1, 2, 3, 4]},
 'size': {1: [2, 4, 9, 7]}}
</code></pre>

<h2> However...</h2>

<p>You should really consider using the <code>pandas</code> library for manipulating data like this. Observe:</p>

<pre><code>&gt;&gt;&gt; import pandas as pd
&gt;&gt;&gt; df = pd.DataFrame({k: pd.Series(v[1]) for k,v in to_sort[0][1].items()})
&gt;&gt;&gt; df
    color    name  order  size
0  Orange  Orange      2     4
1     Red   Apple      1     2
2   Brown    Kiwi      4     7
3   Green   Mango      3     9
</code></pre>

<p>Notice I still had to finagle your original data structure into a <code>pandas</code> DataFrame, but if you start whatever you are doing with a <code>DataFrame</code> to begin with, it will all be much easier. Now you can do cool things like: </p>

<pre><code>&gt;&gt;&gt; df.sort_values('order')
    color    name  order  size
1     Red   Apple      1     2
0  Orange  Orange      2     4
3   Green   Mango      3     9
2   Brown    Kiwi      4     7
</code></pre>

python python-3.x