Sorting a list of dictionaries that contains a list as a value

python sort list of dictionaries multiple keys
python sort dictionary of dictionaries by value
python sort list of dictionaries by date
python sort list of dictionaries descending
sort dictionary by value python
python sort dictionary by value descending
sort list of dictionaries by value python
python sort dictionary by multiple values

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.

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])

Previous article of this segment dealt with sorting list of dictionaries by values using Outputs in same way as sorted() and lambda, but has different internal  c# - How can I sort (Custom Sort) list of Dictionary entry by value - Stack Overflow. My hashtable contains (key, Values[])e.g: myHashtable[keys, Values[]]myHashtable.Add[1, Value1];myHashtable.Add[2, Value2];myHashtable.Add[3, Value3]; myHashtable.Add[4, Value4];myHashtab Stack Overflow.

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.

It may look cleaner using a key instead a cmp: newlist = sorted(list_to_be_sorted, key=lambda k: k['name']). or as J.F.Sebastian and others suggested, Say I have a dictionary and then I have a list that contains the dictionary's keys. Is there a way to sort the list based off of the dictionaries values? I have been trying this: trial_dict = {'*

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)

Python has a built-in function sorted() which sorts elements of an iterable in a given order.sorted(iterable[, key][, reverse]Second parameter is a  Read a XML file that contains a player's name and player's score and store it in a variable. I have been using a Dictionary. Place all these dictionaries into a List. Sort the list of dictionaries by the high score. (can't work this out) Have the ability to add another new score and sort again. Write the results back to the file I loaded it from.

We will follow the steps mentioned below to sort a dictionary using values. Pass the list containing dictionaries and keys to the sorted method. We  Sort dictionary contents by Value. To sort dictionary elements by value we will use the same sorted() function and pass a key function that will return the 1th index element of tuple i.e. the value field from the key/value pair,

In this video I will show you how to use the sort method and sorted function along with the Duration: 7:47 Posted: Sep 5, 2018 Previous article of this segment dealt with sorting list of dictionaries by values using lambda function. This article aims at performing this functionality using itemgetter and showing visible differences. Itemgetter can be used instead of lambda function to achieve the similar functionality. Outputs in same way as sorted () and lambda, but

(The variable was named dict_ because dict is already a builtin.) Starting with Py2.4 the list.sort() method provides a key= argument for doing the  The original list is : [ {‘is’: 2, ‘gfg’: 1, ‘good’: 3}, {‘gfg’: 2}, {‘best’: 3, ‘gfg’: 4}] The values corresponding to key : [1, 2, 4] Method #2 : Using map () + itemgetter () This problem can also be solved using another technique using map () and itemgetter ().

Comments
  • 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...
  • you need to use map to convert your list items to int first
  • @timgeb yes, this is the structure of the list, 3 strings followed by an integer.
  • @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
  • 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. :)