faster and more 'pythonic' list of dictionaries
<p>For simplicity, I've provided 2 lists in a list, but I'm actually dealing with a hundred of lists in a list, each containing a sizable amount of dictionaries. I only want to get the value of 'status' key in the 1st dictionary without checking any other dictionaries in that list (since I know they all contain the same value at that key). Then I will perform some sort of clustering within each big dictionary. I need to efficiently concatenate all 'title' values. Is there a way to make my code more elegant and much faster?</p>

<p>I have:</p>

<pre><code>nested = [
    [
        {'id': 287, 'title': 'hungry badger',  'status': 'High'},
        {'id': 437, 'title': 'roadtrip to Kansas','status': 'High'}
    ],
    [
        {'id': 456, 'title': 'happy title here','status': 'Medium'},
        {'id': 342,'title': 'soft big bear','status': 'Medium'}
    ]
]
</code></pre>

<p>I'd like:</p>

<pre><code>result = [
    {
        'High': [
            {'id': 287, 'title': 'hungry badger'},
            {'id': 437, 'title': 'roadtrip to Kansas'}
        ]
    },
    {
        'Medium': [
            {'id': 456, 'title': 'happy title here'},
            {'id': 342, 'title': 'soft big bear'}
        ]
    }
]
</code></pre>

<p>What I tried:</p>

<pre><code>for oneList in nested: 
   result= {}
   for i in oneList:        
       a= list(i.keys()) 
       m= [i[key] for key in a if key not in ['id','title']]
       result[m[0]]=oneList
       for key in a:
            if key not in ['id','title']:
                del i[key]
</code></pre>

<pre><code>from itertools import groupby    
result = groupby(sum(nested,[]), lambda x: x['status'])
</code></pre>

<p>How it works:</p>

<p><code>sum(nested,[])</code> concatenates all your outer lists together into one big list of dictionaries</p>

<p><code>groupby(, lambda x: x['status'])</code> groups all your objects by their status property</p>

<p>Note itertools.groupby returns a generator (not a list), so if you want to materialize the generator you need to do something like follows.</p>

<pre><code>from itertools import groupby    
result = groupby(sum(nested,[]), lambda x: x['status'])
result = {key:list(val) for key,val in result}
</code></pre>

python dictionary