hi there Serdar Yegulalp here for Infoworld at IDG. Today in Smart Python we're looking at the
ways lists in python can be sorted there's two ways you can sort lists in Python one by
generating a new sorted list from the old one and two by sorting an existing list in place
let's look at each of these in turn because they both have drastically different behaviors
python has a built-in function called sorted that lets us take a list and generate a new sorted
list from it it also works with other object types but for now we'll concentrate on lists
so consider a list of randomly generated numbers using this script if we pass the
list to sorted we get back another list of the same numbers this time in ascending order
we can also sort in descending order by just passing the reverse argument to sorted
now for numbers and strings python assumes a common sense default sorting order
they sort pretty much as you expect alphabetically and in rising numerical order but if you want
a custom sort order or you're dealing with objects that don't seem inherently sortable
you need to deal with that a little differently with sorted you can provide an argument named key
key indicates a function that each sortable item gets passed to that function will yield up a value
that sorted can then use in turn to determine the sort order for the item in this example we're
using a function that determines if the item in question is greater than five that will yield a
boolean value greater than five less than five and since booleans sort in the order of false to true
that means that all the numbers in the list less than five get sorted to the front all the numbers
greater than or equal to 5 gets sorted to the back notice that there's no other sorting within
those two groups it's just being sorted by whether they're less than or greater than or equal to 5.
the other way that you can sort lists is in place this means that you're not generating
a new list you're just taking the existing list and sorting the items directly inside it
to sort in place you use the sort method which is available to all list objects so all we have to
do to sort a list in places take our list and invoke sort on it sort will also take the key
and reverse options just like sorted did and will yield the same results now bear in mind sorting in
place has a major behavioral difference compared to creating a new sorted list the biggest is that
it is a destructive action you are operating on the original list so its original ordering will be
gone forever remember this if you want to work with the original data at some point you may
be better off making a copy by way of sorted if you keep wanting to keep the original data
another corollary rule of this is performance if you have a very large list with thousands of items
it can be much more efficient to sort it in place using sort than to sort it by way of a new list
using sorted this way you're not going through the overhead of creating a new list and that
can really add up if you're doing that many times over in a tight loop one final note about sorting
is that whichever method you choose the sorting method will only need to pass over the list once
the downside of that is again if you have a longer list it will take longer to sort but at least it
won't need to traverse the list more than once to do it again the main reasons to keep this in mind
are performance if you have a very large list that needs sorting you'll probably want to sort
it in place and you'll probably want to keep the amount of sorting action to a minimum finally when
you sort a list you can't use default sorting on a list that has a mixture of items a list of both
strings and integers for instance can't be sorted you'll get a type error if you try to do that
but you can use the key function as a way to get around this problem in this example i have the key
function use the integer value of each element in the list as its sort key it converts each element
to an editor assuming it can be converted note that this does not change the elements in the list
into integers it just uses their integer values assuming they can be converted at all that's
why i have this tri-accept block which catches anything that can't be converted into an integer
and just renders it as 0 such as the none value that's it for this episode if you like this
video leave a comment below and don't forget to subscribe to our idg techtalk channel on youtube
and for more smart python be sure to follow us on facebook youtube and infoworld.com you