1

I have this list of dictionaries which have a list as the value:

>>> lod
[{'A': ['100', '200', '300', 600]}, {'B': ['1000', '2000', '3000', 6000]}, {'C': ['1', '2', '3', 6]}]

and basically, I need to sort this list based on the last item in every dictionary's list.

So I used sorted() but it didn't work as expected:

>>> sorted(lod, key= lambda x: lod[0][''.join(list(lod[0].keys()))][3])

[{'A': ['100', '200', '300', 600]}, {'B': ['1000', '2000', '3000', 6000]}, {'C': ['1', '2', '3', 6]}]

What do you think is the problem here?

Thanks.

3
  • 2
    Is it correct that every last element of your lists is an int but the previous elements are strings? This data might need some preprocessing... Commented Oct 21, 2018 at 21:32
  • you need to use map to convert your list items to int first Commented Oct 21, 2018 at 21:34
  • @timgeb yes, this is the structure of the list, 3 strings followed by an integer. Commented Oct 21, 2018 at 21:36

3 Answers 3

2

The problem is that this is not how the key parameter works. It should be a function that's used like this in the sorting algorithm:

if key(a) < key(b):
    # sort this way

So, it needs to return the value you want to be compared:

sorted(lod, key=lambda elem: next(iter(elem.values()))[-1])
Sign up to request clarification or add additional context in comments.

2 Comments

@timgeb, that's only because one can't index dict_values objects for some reason (I think Python 3.7 should remove this restriction since dicts are now ordered; maybe it already did - I'm on Python 3.6 right now), but can iterate over them.
yes, that's why I casted the values to a tuple before indexing
0

If we assume that all your dicts have exactly one (key, value) pair and that the last element of the value-list is an integer, the following solution should fit your criteria.

>>> l = [{'A': ['100', '200', '300', 600]}, {'B': ['1000', '2000', '3000', 6000]}, {'C': ['1', '2', '3', 6]}]
>>> 
>>> criterion = lambda dict_: tuple(dict_.values())[0][-1]
>>> sorted(l, key=criterion)
>>> 
[{'C': ['1', '2', '3', 6]},
 {'A': ['100', '200', '300', 600]},
 {'B': ['1000', '2000', '3000', 6000]}]    

tuple(dict_.values()) constructs a one-element tuple of the values for each inner dict, [0] gets the list out of that tuple and [-1] gets the last element of that list as the sorting-criterion.

2 Comments

PEP-something recommends you defined named functions explicitly instead of assigning labels to lambda functions.
@jpp yes, and l is a bad variable name. It's a demo code snippet. :)
0
sorted(lod, key=lambda d: list(d.values())[0][-1])

will do it. Explanation: list(d.values())[0] will return the only value from your dictionary, [-1] will return last item of it (int value used for sorting)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.