In this Python beginner Tutorial we are using the Pycharm IDE and Python version 3. We go in depth on how to use python's built in sort() function to manipulate data in lists.
Strings in lists default to being sorted alphabetically, while integer or float numerical values default to being sorted from smallest values first to highest values last. We show in this video how you can use the reverse option when sorting lists to switch it such that the smallest values come last and largest come first or sort in reverse alphabetical.
Next we demonstrate how to use a function to sort a list by a value that is not the default method for that data type. We begin by sorting a list of strings by the length of the entry, but follow by sorting a list compiled of dictionaries and sort by the value of a specific key!
There are tons of uses for sorting lists and hopefully this covered what you needed, but if you have any additional questions or want to see something specific next just let us know in the comments!
what's going on you guys welcome back to
the channel i've got another python
tutorial for you today and the concept
in this video we're covering is how to
sort a list using python's built-in sort
function so uh before i get started if
you do find this or anything else on the
channel useful i really appreciate a
like and a subscribe to the channel it
helps me out a ton and if you have any
questions or want to see something in a
future video just be sure to let me know
about in the comments and without any
further ado let's get into it
so you can see i've created a few lists
and lists can be anything in python they
can be strings they can be numbers they
can be dictionaries so i created a few
different
a few different lists with strings and
then a few different
um
data types as well so i have years which
these are integers obviously you could
do it with floats and then i have a list
of dictionaries and so we're going to go
through and sort all of those so it's
really easy to use the sort function
it's you just type in the name of your
list and then dot sort and if you don't
put anything in those variables
in the in the
conditionals after sort it's going to do
its default sorting which
let's cover that by looking at what it
does for fruits here
so fruits is a bunch of strings
the default for strings is going to be
sort in order of alphabetical so
starting with a
and then you can see with these
particular fruits you go a b c
g k where the original list did not have
them in that order so you can see it's
alphabetical
and then the default for like numerical
so let's do the same thing with years
it's it's even if it's numerical even if
it's dictionaries um whatever is in your
list it's just list name dot sort and
then your parentheses
um but then if you do it with something
that's numerical
it is going to do it um
least smallest number uh going up to the
greatest number so that's called
ascending
um but what you can do is this
years.sort and then you can put
conditions in here
like
reverse equals true and so by default
reverse is false which makes it the
default condition but if you do it this
way
and you probably guessed this it's going
to do just the opposite of whatever you
did so if it started before you can see
when we do it the first time it goes
least to greatest in ascending order but
then we reverse it and it goes in
descending order so now if we look at
our car list so let's do car.sort
and let's reverse there to see what it
does with strings okay
so let's go ahead and take a look at
that so you can see it does reverse
alphabetical order so um just keep in
mind if it's uh if it's integers if it's
floats if it's decimal values and if
it's strings
it's going to be ascending
from the beginning of the alphabet or
the smallest number up to the largest
by default but you can
set the reverse condition to get it to
flip
and then the next thing i want to talk
about is you can also sort a list using
a function so let's take our classmates
here you can see we have just a list of
a lot of different students
and let's say we actually want to sort
the list but we want to sort it based on
the length of the student's name and so
rather than alphabetical which is the
default that we would get we can create
a function to sort this so what we'll do
there is we'll define a function and
we'll call it the sort function
okay
and it's going to take one item at a
time because what we're going to do is
we're going to pass our list in here one
item at a time for the sorting function
and it's going to return a value so what
we're going to return out of our sort
function
is the length of the item as it gets
passed in so we're just going to use
this function to return a value saying
how long each piece of the
each piece of the list that got passed
through is
so um the way you actually call that
function now is you do classmates
dot sort just like before
and then you do key
equals
and then here's where you call that
function
and
that and actually you don't need these
additional parentheses so what you do is
you just say key equals sort function
and it knows um this is kind of unique
to the sorting function you
are always going to be passing in one
item at a time so that's the point of
key equals sort function and you don't
need to like add another set of
parentheses here and type in item that
that
doesn't work for lists so this is how
you use this and let's go ahead and
print classmates now after that and what
we should see
is rather than being alphabetical like
strings normally are it's based on the
length of the student's name so you can
see we go three four five
five again six
uh seven and eight characters so that's
really cool another nice thing you can
combine these two uh things that we've
covered so if you want to do it by
length but you also want to say
and i want to see who has the longest
name first you can do reverse equals
true and a key on the same function
so that's really useful in my opinion a
lot of the times you'll want to combo
these and and do descending order based
on a function and sometimes it's just
easier to write a function
that's just one line and it just returns
the length rather than like
finding a way to do all that math inside
your function so this is really useful
and then the last thing i wanted to do
and the reason i made a list that was
all dictionaries so you can see we've
got key value pairs
of a meal name and then a dish and then
a second item of like the time that you
would eat and then the time in like
military time
so
that's dictionaries and you can still
sort it so let's create another function
that we'll call sort dictionaries and
let's say that you want to return
an item inside you want to return not
like the length of it now you want to
return
an item
at a specific value so uh
you want to
look up
the time that you're oop my typed item
you want to look up the time that uh
that that meal is served and you want to
order it based on then so what we're
saying here is you pass in each
dictionary
one at a time check for the key value of
time and then return that value so you
can see here these are in military time
right
and they're not ordered it goes dinner
then breakfast then lunch but we want it
to go breakfast lunch dinner so we're
saying sort this list based on what time
the meal is served
and then let's go ahead and
dictionaries.sort
and what we're going to do now is we're
just going to call that function just
like before the key
that we made is called sort
dictionaries and you don't have to pass
an item in
and let's go ahead and print it so you
can see what's going on here
print dictionaries okay
so right there you can see we just
reordered our list to go breakfast lunch
and dinner okay so
um this you know it's a quick video it
just really covers one concept but i
know there's been a lot of questions on
this on my channel i've seen it on other
videos
sorting a list is a really useful way to
process large sets of data and if you
know just really the key the way to use
a function to do some more advanced
sorting for you and then reverse if you
have it in the opposite order of what
you want those are the two most common
tools you'll use outside of just the
default sort
which is already really useful so
hopefully you found this useful if you
have any questions on this or you want
to see me cover something in a future
video be sure just let me know about in
the comments below leave a like on the
channel and if you would would want to
subscribe the channel i appreciate that
a ton it helps me out this is a new
channel i'm trying to get going so thank
you all for your support and your views
and as always good luck with your code
and thanks for watching thanks bye