Sort

TwitterFacebook
Get flash to fully experience Pearltrees

"To sort a dictionary" « Python recipes « ActiveState Code

The concept of 'sort' applies only to a collection which has _order_ -- a sequence; a mapping (e.g. a dictionary) has NO order, thus it cannot be sorted. Still, its keys can be extracted as a list, which can then be sorted. The example functions return the values in order of sorted key , which just happens to be the single most frequent actual need corresponding to user questions such as "how do I sort a dictionary":-) The implementation choices are interesting. Since we are sorting key-value pairs by the key field, then returning the list of value fields, it seems clearest (conceptually simplest) to architect the solution as in the first example: .items, .sort, then a list comprehension to pick the value fields. However (at least on my machine) this turns out not to be fastest: extracting just the keys, sorting them, then accessing the dictionary for each key in the resulting list comprehension, as in the second example, appears to be speedier. http://code.activestate.com/recipes/52306-to-sort-a-dictionary/
The results from the above examples are in ascending order. To sort in descending order, attach reverse=True at the end of the sorted() function. http://selinap.com/2009/02/how-to-sort-python-dictionary/

How to sort Python dictionary

In Python how do I sort a list of dictionaries by values of the dictionary? - Stack Overflow

http://stackoverflow.com/questions/72899/in-python-how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary 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.
http://stackoverflow.com/questions/1143671/python-sorting-list-of-dictionaries-by-multiple-keys But I have to run this through a function, where I pass in the list and the sort keys. For example, def multikeysort(dict_list, sortkeys): . How can the lambda line be used which will sort the list, for an arbitrary number of keys that are passed in to the multikeysort function, and take into consideration that the sortkeys may have any number of keys and those that need reversed sorts will be identified with a '-' before it?

Python sorting list of dictionaries by multiple keys - Stack Overflow

Many languages implement simple arrays (lists in python) that are keyed by a integer. For example, if you made a list [1, 2] – the first value would be retrieved via [1, 2][0]. my_list = [1, 2, 3] my_list[0] # Out: 1 my_list[1] # Out: 2 http://yuji.wordpress.com/2008/05/14/python-basics-of-python-dictionary-and-looping-through-them/

Python — Basics of Python Dictionary: Looping & Sorting « Useful Stuff.

http://blog.markturansky.com/archives/130 I use Python all the time for quick little scripting tasks. There’s nothing better to slice and dice a file, so I use Python for a lot of reporting tasks. That usually involves building some kind of data structure in my script that I’m slicing and dicing from files. In my work, I have a LOT of units of work processing in parallel on a grid. I have GUIDs tagging each unit of work, and that GUID is the perfect key for a Map/Dictionary data structure.

Mark Gregory Turansky » HOWTO: Sort a Python Dictionary/Map