hello and welcome to another Python
tutorial in this tutorial we're going to
be discussing how to order a Python
dictionary just as an aside
fundamentally a Python dictionary
actually has no order so to speak and so
if you sometimes you try to like look
this problem up online you might find
people tell you that that is simply not
possible but it is possible but it's
just kind of like a hackish way about
doing it because a lot of times people
you know store stuff in dictionaries
because you key in value just seems to
make a lot of sense instead of you know
building a list of arrays a dictionary
is just really really quick and easy but
sometimes you want to order that in some
form or fashion so what we're going to
do is have a dictionary of names and
ages and I'm going to show you guys how
you can kind of order that so to start
there is no import or anything that you
need but we are going to at least define
our dictionary which we'll call epoch
dict if you have not seen the tutorials
on just dictionaries alone definitely go
check that out before this one so we're
going to make this dictionary and within
the dictionary we're going to have the
ages our names and then age and so I'm
just going to copy and paste this since
this is what we're going to use so the
first person will be Jack and Jack will
be five years old and then let's just
say we're going to do one two three we
have four more people then we're gonna
have a bill Bill is going to be 14 then
we're going to have a cat cat cats three
we're going to Jess Jess is 33 then
we're gonna have an Alex and Alex is
four so as you can see both
alphabetically and numerically we're not
in any good order get rid of that last
comma there so there's two ways that you
can order a dictionary anyway so first
one we're going to say is sort by we'll
call this a function and it's going to
be sort by key so that's going to sort
by the first value because you
you know key and then a value so the
first like name basically is what we're
going to sort by and the way you do this
is we'll define our variable so say sort
by key dict equals and you use this
sorted bit the function and the way you
do this is you're going to say epic Dix
die items and then you're going to say
the key equals and lambda if you haven't
viewed my tutorial on lambda I do have
one so watch that if you're confused
about what's going on here but basically
we're justifying it's a lambda just a
one-line function so and then this is
the parameter of that function this is T
0 so this is just saying the 0 with
element from the key and the 0 element
is going to be Jack so it's going to
sort by this element but again if you're
confused maybe go check out that lambda
tutorial and finally what we're going to
do is happening to my formats there we
go and then we'll just print out sorted
by key dates okay
so we'll save that and we'll run it and
we'll get this error here I guess that's
why we let's see what do we do we put
Jess okay we've just 33 that's why my
mouse was acting all weird I think
anyway save that we'll run it and we'll
drag it on over here and then we'll say
sort by keys up call it yeah so that's
our function is sort by key run it and
too many typos too many typos okay sort
by key
run it and as we can see now it's
organized by the key so Alex so a B je
je mrs. J AJ and Kat
so obviously that was a success but if
you do notice no longer is it really a
dictionary this is actually an array
with a list inside the array so just
keep that in mind it's not it it's not
that we it's not like we reordered the
dictionary because again a dictionary
has absolutely no order so the only
circumstance where you might want to
order a dictionary is if you're going to
create a list from the dictionary for
some other purpose right then you'll
want to order the dictionary but you'd
never order a dictionary to create an a
quote-unquote ordered dictionary because
it's just frankly not possible so anyway
that's sort by key now let's say you
want to sort by the value that comes
from the key so if you pay attention to
what this lambda function meant if
you're following you should be able to
guess what's going to come for this
lambda function but if not here we go so
define sort by value empty params I'm
going to say sorted by valued x equals
sorted epic dick dot items let's not
typo that again and then we're going to
say this time key equals lambda t colon
T and it's going to be one or the second
element so again it's kind of like
pulling this apart and it's calling the
key this time it's saying the key is
indeed this element here and then it's
going to what we're going to tell it to
do is we're going to say go ahead and
print out sorted by value value Dix save
that we'll run it drag it on over here
so we can see it sort of by value is the
name of our function so sorry value call
it mugginess
we called the wrong thing we'll run it
one more time it's just sort by value
you drag this up so you can see our
production and as you can see this time
instead of organizing it by the first
one it's organizing it by the value
which is three then four then we've got
a five then a fourteen ten to thirty
three again it's not a dictionary
instead it's actually the zeroth element
of an array that's containing an ordered
list because you can't order lists very
easily but this is just kind of a way
around it say your say your body of
information is a dictionary which it
very well may be in the near future
that's how you're going to order those
things so anyways hopefully that helps
some of you guys out because if you do
look for this answer online a lot of
times you're going to have people just
say aye that's not possible and well it
is possible and here's how you how you
would do it fundamentally yeah it's not
possible because you there is no order
to a dictionary but really this is the
answer that you're looking for and it is
actually pretty helpful to be able to do
this so anyway hopefully I've helped
some of you guys out stay tuned for more
videos