sorting on keys in a list of dicts
Jp Calderone
exarkun at divmod.com
Thu Jan 6 09:40:01 EST 2005
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
More information about the Python-list
mailing list