androde
androde

Reputation: 41

Sorting a list of dictionaries by key in Python

I have a list of dictionaries in Python which each contain just one numerical key, and I want to sort them by their keys. Example:

list = [{.56: 'a'}, {1.0: 'a'}, {.98: 'b'}, {1.0: 'c'}]

I want to sort this and return something like this:

[{1.0: 'a'}, {1.0: 'c'}, {.98: 'b'}, {.56: 'a'}]

In the instance where the key values are the same, I don't care how those are sorted. I've tried using .sort() or .sorted(), but I'm having trouble figuring out the arguments.

Upvotes: 3

Views: 370

Answers (4)

John La Rooy
John La Rooy

Reputation: 304473

This is a simplified version of @dkamins

>>> lst = [{.56: 'a'}, {1.0: 'a'}, {.98: 'b'}, {1.0: 'c'}]
>>> sorted(lst, key=max, reverse=True)
[{1.0: 'a'}, {1.0: 'c'}, {0.98: 'b'}, {0.56: 'a'}]

recall that max(d.keys()) returns the same result as max(d)

lambda d: max(d) just wraps another function around the call to max so we can leave that out

Upvotes: 3

dkamins
dkamins

Reputation: 21948

This will work in Python 2 and 3:

>>> mylist = [{.56: 'a'}, {1.0: 'a'}, {.98: 'b'}, {1.0: 'c'}]

>>> sorted(mylist, key=lambda d: max(d.keys()), reverse=True)
[{1.0: 'a'}, {1.0: 'c'}, {0.98: 'b'}, {0.56: 'a'}]

It uses the sorted's key argument to sort based on the largest key of each dict, which in this case will just be the first one if you only have one.

The other solution may work, but this is much more straightforward.

P.S. Don't ever name your variables after Python builtins like list.

Upvotes: 2

jayelm
jayelm

Reputation: 7698

With Python 3+, since it throws an error as @Simon mentions. This depends on your dictionaries being singletons:

>>> lst = [{.56: 'a'}, {1.0: 'a'}, {.98: 'b'}, {1.0: 'c'}]
>>> sorted(lst, key=lambda d: d.keys()[0], reverse=True)
[{1.0: 'a'}, {1.0: 'c'}, {0.98: 'b'}, {0.56: 'a'}]

Upvotes: 1

Simon
Simon

Reputation: 10841

At least in Python 3.3, @Hai Vu's comment does not work, because it raises a type error since dict() is not an orderable type. However, the comment suggests an answer if we can convert the dictionary into an orderable type, like a list of tuples for instance:

>>>> L = [{.56: 'a'}, {1.0: 'a'}, {.98: 'b'}, {1.0: 'c'}]
>>>> L2 = sorted([list(zip(x.keys(),x.values())) for x in L],reverse=True)
>>>> L2
[[(1.0, 'c')], [(1.0, 'a')], [(0.98, 'b')], [(0.56, 'a')]]
>>>> [{k: v for (k,v) in x} for x in L2]
[{1.0: 'c'}, {1.0: 'a'}, {0.98: 'b'}, {0.56: 'a'}]

We start with the given data, assigned to L rather than list to avoid confusion with the list keyword. Then we use a list comprehension that creates a list of lists of tuples, where each of the sub-lists is the zipped-together keys and values of each dictionary. As lists, those can then be sorted by the sorted() function.

Unfortunately, we don't have a list of dictionaries any more so the next step is to convert from a list of lists of tuples back into a list of dictionaries using a dictionary comprehension inside a list comprehension to convert each list of tuples into a dictionary and then return them all together as a list.

Upvotes: 0

Related Questions