How do you sort a dictionary in a list in python by value?
The Show
Alternatively, you can use
For completeness, add
wjandrea 24.6k8 gold badges53 silver badges73 bronze badges answered Sep 16, 2008 at 14:39
14
To sort the list of dictionaries by key='name':
To sort the list of dictionaries by key='age':
cedbeu 1,83914 silver badges24 bronze badges answered Sep 16, 2008 at 15:18 4
Or better: Since Python 2.4, there's a
...the lambda is, IMO, easier to understand than answered Sep 16, 2008 at 14:39
pjzpjz 40.6k6 gold badges48 silver badges60 bronze badges 3 If you want to sort the list by multiple keys, you can do the following:
It is rather hackish, since it relies on converting the values into a single string representation for comparison, but it works as expected for numbers including negative ones (although you will need to format your string appropriately with zero paddings if you are using numbers). 4
'key' is used to sort by an arbitrary value and 'itemgetter' sets that value to each item's 'name' attribute. answered Sep 16, 2008 at 14:43
efotinisefotinis 14.1k5 gold badges31 silver badges36 bronze badges I guess you've meant:
This would be sorted like this:
answered Sep 16, 2008 at 14:36
You could use a custom comparison function, or you could pass in a function that calculates a custom sort key. That's usually more efficient as the key is only calculated once per item, while the comparison function would be called many more times. You could do it this way:
But the standard library contains a
generic routine for getting items of arbitrary objects:
answered Sep 16, 2008 at 14:52
OwenOwen 8716 silver badges6 bronze badges Using the Schwartzian transform from Perl,
do
gives
More on the Perl Schwartzian transform:
1 You have to implement your own comparison function that will compare the dictionaries by values of name keys. See Sorting Mini-HOW TO from PythonInfo Wiki answered Sep 16, 2008 at 14:31
MatejMatej 5,8202 gold badges27 silver badges26 bronze badges 2 Sometimes we need to use
1 Using the Pandas package is another method, though its runtime at large scale is much slower than the more traditional methods proposed by others:
Here are some benchmark values for a tiny list and a large (100k+) list of dicts:
2 Here is the alternative general solution - it sorts elements of a dict by keys and values. The advantage of it - no need to specify keys, and it would still work if some keys are missing in some of dictionaries.
1 If you do not need the original Key function:
The
Sorting it in-place:
If you need the original
Printing
Let's say I have a dictionary
Check this out. I have been a big fan of a filter with lambda. However, it is not best option if you consider time complexity. First option
Second option
Fast comparison of execution times
If performance is a concern, I would use From https://wiki.python.org/moin/PythonSpeed:
Here is a comparison of sorting speed using
Both techniques sort the list in the same order (verified by execution of the final statement in the code block), but the first one is a little faster. As indicated by @Claudiu to @monojohnny in comment section of this answer,
to sort the list of dictionaries by key
or, likewise
sorting by multiple columns, while in descending order on some of them: the cmps array is global to the cmp function, containing field names and inv == -1 for desc 1 for asc
How do you sort a dictionary within a list Python?To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function.
How do you sort a dictionary by value?Need for Sorting in Dictionary. First, sort the keys alphabetically using key_value. iterkeys() function.. Second, sort the keys alphabetically using the sorted (key_value) function & print the value corresponding to it.. Third, sort the values alphabetically using key_value. iteritems(), key = lambda (k, v) : (v, k)). Can we do sorting in dictionary in Python?We can sort lists, tuples, strings, and other iterable objects in python since they are all ordered objects. Well, as of python 3.7, dictionaries remember the order of items inserted as well. Thus we are also able to sort dictionaries using python's built-in sorted() function.
Can we sort dictionary values?It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list—probably a list of tuples.
|