
Sort
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.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.
How to sort Python dictionary
In Python how do I sort a list of dictionaries by values of the dictionary? - Stack Overflow
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

