

In this How To article I discussed sorting of collection-like Python data structures such as tuples, dictionaries, and lists and provided several concrete examples of both simple and moderately complex sorting use cases you are likely to run into as a Python developer.Īs always, I thank you for reading and feel free to commend or critique in the comments section below. This enables providing continued free tutorials and content so, thank you for supporting the authors of these resources as well as Conclusion earns commision from sales of linked products such as the books above.
SORTED PYTHON LIST OF DICTIONARIES CODE
Fluent Python: Clear, Concise, and Effective Programming is a must read for any Python developer serious about code craftsmanship.Python Tricks: A Buffet of Awesome Python Features is truly a fantastic collection of Python productivity hacks.> rev_age_name_sorted_people_tups = sorted(people_tups, key=lambda p: (p.name, p.age), reverse=True) Same as part (ii), but sorted in alphabetical order by the value. Display both the keys and values sorted in alphabetical order by the key. Create a dictionary and display its list-keys alphabetically.

> d = ]Īnd for completeness I show the namedtuple example also. Here are the major tasks that are needed to be performed sort a dictionary by value and keys in Python. > tup = (8, 3, 4, 1, 5)Ī little more thought and decision goes into sorting of dictionaries due to the key / values nature of the data structure but, unlike the list data structure dictionaries don't have a built in sort() function so you are limited to sorting only the keys or the values of a dictionary with the later needing explicit syntax.īy simply calling the sorted() function on a dictionary a sorted list of the dictionaries keys are returned which can then be used to iterate over and access the original unchanged dictionary. Your only choice is to use the sorted() function and create a sorted copy of the input tuple. Sorting tupes is actually less complex due to the fact that they are immutable data structures thus their is less to consider when you want to sort them. Then follow with list.sort() which, again, sorts the list in place. TypeError: '> rev_sorted_copy = sorted(letters_to_sort, reverse=True) One important thing worth noting with sorting is that the collection being sorted must be homogenous (ie, all the same data type such as all string or all ints) or the sort function will throw and error. > from copy import copyĪs you likely noted the above lists of letters we sorted lexographically (aka, alphabetically) with capital letters given higher precendence. (ii) list.sort() method on the list object itself which sorts the list in place. (i) the sorted(collection) function which takes a list as an argument and returns a sorted copy of the list > letters_to_sort = I then uses the index order to sort all of the lists in the dict.

Lists can be sorted in one of two approaches using either: It determines the sort order by getting the indices of the sorted arrival times. The most commonly sorted data collection-like data structure is the list. Simple Sorting of Lists with sorted() and list.sort()
