sorting on keys in a list of dicts

J Berends j.p.t.j.berends at [N0SP4M].nl
Thu Jan 6 16:05:57 CET 2005


Jp Calderone wrote:
> On Thu, 06 Jan 2005 15:31:22 +0100, J Berends <j.p.t.j.berends@[n0sp4m].nl> wrote:
> 
>>Suppose I have a list of dictionaries and each dict has a common keyname 
>>with a (sortable) value in it.
>>
>>How can I shuffle their position in the list in such way that they 
>>become sorted.
>>
> 
> 
>   In Python 2.4,
> 
>     import operator
>     L.sort(key=operator.itemgetter(key))
> 
>   In Python 2.3,
> 
>     L2 = [(d[key], i, d) for (i, d) in enumerate(L)]
>     L2.sort()
>     L = [d for (v, i, d) in L2]
> 
>   Jp
the 2.3 version seems to work quite nicely! thanks. Using the 2.3 for 
version compatibility. Thanks!



More information about the Python-list mailing list