Member-only story
Advanced Sorting in Python Using Lambdas and Custom Functions
Sort lists of elements of non-basic data types (e.g., int, str)
Published in
3 min readFeb 24, 2020

Basic Sorting
When it comes to sorting, the most-used Python functions are sorted()
and sort()
.
Although there are some differences in their usage, memory usage, and operation speed, as discussed in a previous Medium article, both functions can be used to sort a list
. Below is a simple example.
In the above example, there are a few things to highlight for those who are less familiar with the sorted()
and sort()
functions.
- The
sort()
function is actually an instance method of thelist
data type, such that the usage islist.sort()
. - The sorting operation by
sort()
is in place and returnsNone
. Thus, if you check the type by runningtype(numbers.sort())
, the output will beNoneType
. - Unlike the
sort()
function, thesorted()
function has better flexibility by allowing the argument to be alist
or any otheriterable
…