Detailed Notes
This episode of python certification walks through examples of dictionaries and related functions.
Useful Links: https://www.programiz.com/python-programming/dictionary
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
one well hello and welcome back we're continuing our series of episodes looking at python certification this episode we're going to look at dictionaries we've not really i don't think we've touched on these yet if you're doing this in order so we're going to get sort of step back and go to very basic stuff talking about dictionaries very similar to well in some ways similar to lists but there's also some differences now we're going to work with this one i've got a sample dictionary and this is how you create one essentially a fully populated dictionary you're going to have a key and you're going to have a value and then you use comma to separate here's another key another value another key number of values now let's start with the most basic and let me do just so when you have that dictionary and print it out then you'll see that it prints it out almost identical to what it looks like in the code so there's our keys and our values now another thing we can do is we can we can actually print out the keys and print out the values so if we do that we see here here's the diction and when we do notice when we do keys it actually says dictionary keys it's going to print that out so you have home office and bill those are the three keys that i created and then if you do dictionary values it does same thing as it tells you that's the values and here are the ones that are within that so that's how you that's the uh the usefulness basically the dictionary is it's just a bunch of key value pairs and there's plenty of opportunities for us to use that in just about any solution we run into now once you if you want to work with specific items there's actually two ways to do it you can uh let's do this let's do that and so the two ways we can do it we can either look at it by key and treat almost like an array where the key is index or we can do a get now one of the things we're going to find out is let's say we'll follow that and let's see if we do something that does not exist let's call it red and then see what happens in those cases so in these two cases first two cases we should get home which is one two three main street let's see what happens when we get when we try to do uh red ah exercise so here we go so we got one two three main street we get one two three main street however when we do it blows up right here when we try to do sample red then it gives us an error so we can't do it uh if it doesn't exist because it's going to give an error which is it's a key error let's just keep the same thing and do a get let's see what it does here notice here when we do the get we don't get an error in that case it figures out that hey that doesn't exist so i'm going to send you a none i'm not going to actually throw an error i'm going to allow you to do it but i'm going to set none back that basically says i can't find a value for that so generally speaking we're going to want to use get over this this other construct basically to retrieve items now we can do some things so we could do like four um assume value in sample.values actually let's do this let's do four key and sample dot keys and then we can do print let's see key plus address is and then we can do plus um sample dot get key uh just get it close that so we can we can do some of our some things we've seen before to work within these so we can see home address is this and so this is our key we do some stuff and that's our value that's often how you're going to walk through a dictionary is probably going to do like you know four value in either going to go through the values or through the keys but of course if you do the keys then you can very easily do a get to get a specific value now another thing you can do is uh let's see let's just print sample you can use those keys to change them so they are not immutable so if i do sample let's see home and then let's make it uh let's just do it four three two one whoops some range which makes just have some little random thing and then we'll see that so now with this one which is really where we do need to use this uh this construct to address stuff is we can actually reassign it so now if we look uh we can see here so we come through we do our little loop and then here we changed out our value and assigned it to something new so we can use that to change things out now we can we can also do let's do this let's make this let's do vacation this will be uh sunshine avenue sounds like a nice little so now note we don't have that but we can come in and take sample and extend it so here we add a vacation and it's just going to give us um it just appends by default because key order doesn't really matter but it's going to pin that new key in so we could add actually multiple things we could create a dictionary like let's do this let's call it from blank and we're just going to call blank equals an empty dictionary and then we can do blank um let's just do it this way a equals let's just do this apple and then uh print blank and i'm gonna do it again and we'll see that we can actually build something out from scratch like this okay and then we'll see so then it goes apple let's see adds that in and it comes back in and it adds banana another thing we can do is uh this will seem familiar if you've gone through the lists is that we can do a pop so we can do let's do it off of sample so we're going to do sample and we're going to do it much like we did before see so if you do a dictionary pop there's two of them in this case it's a little different normally you can either do you're going to do it by key so if you do a pop it's normally going to need a key value so i can specifically pull off let me go back to vacation and let's see what comes out and then see what sample looks like afterwards so if we do that then oh here we go so when we do the pop it returns a value we pop much like we saw in a list and then also we can see now that it pulled it out of the dictionary much like just the same way it did list it actually pulled that thing out of it now if we do a pop item and we'll just continue with that then it's going to just pick something so let's see what happens we're gonna do a pop item twice and let's see what it grabs so we do it the first time it pulls financial way which happens to be the last item and then and then it pulls it out and then the second time we do a pop it's going to pull the last item again and it gives us just this one left so much like um although it may be a little tricky where the keys are you don't necessarily know what the order is uh unless you you sort of know the value with which you've added the order with which you've added stuff into the dictionary but it's going to effectively work like a pop and list where it's going to say whatever is the last thing that came in that's the first one i'm going to pull out so that's how you can you can do a pop either a specific key or you can do a pop item as we saw here let's see um so we also did oh one we did not look at is items so let me go back to that i forgot that one in there because we looked at keys and we looked at values and now let's look at items which is another way so we could easily do that somewhere up here so we could do like item in sample night item so let's go see what items gives us because that's another one we want to make sure we know uh and as we can see here oops let me do this beforehand so let's move these pops afterwards so now you can see items is when it sends it out it's going to show you it gives you the list so this is a comma separated list of the items much like you would see uh like you see with the and it says here it's items so you you see each of those comma separated values within the the display uh now it's items it's a keep it's a key value pair so that also means that if i were to take that and just to make it a little more blatant if i take this loop and i do item in sample dot items i'm just gonna do uh let's do the next pair is and it's just gonna do the item uh let's get rid of that and whoops oh i cannot cat oh so i have to convert that to a string which i think i can do by this let's see if i can do that with a tuple can i i can okay so i convert the tuple to a string and um here we go next pair so what we had here is we had lonesome range office and billing and you see that it pulls that whole pair out for each tuple and then we can do something with it and so let's say if we wanted to try to just take the first item let's just see what it does there because that's actually a good one to note and so now if i just try to take tread like an array and take the zero based item then um so it should go next pair is and you'll see here where i don't actually have to there's behind the scenes uh there is a an order to it to some extent so i can just take in this case the first item so i'm taking the first key here item 0 is going to be the key for this item if i try to do item one then i'm saying i don't care what it is in this tuple so that's just zero and one zero and one and so it's going to take those and of course if i try to push it then it's going to give me issues because uh here we go where'd it go yeah tuple index out of range so there's only two in there so i could work with it that way if i don't really know the key or the value then i could just treat them as item zero and item one and go from there but typically you're going to send that out as some sort of a tuple and then and work with it that way so items gives you keys gives you the keys the left side of the equation values gives you the right side of the equation and then items gives you both it gives you each tuple the two items that you see in the dictionary so yes basically between each comma so that's my tuple key value here i have office in 22 baker street office the key 22 baker streets of value so on so i have keys values and then i can do items if i want to look at them that way let's see another thing we can do that we have seen before is we can do a clear let's do it this way let's go ahead and clear it and then print it so if we do a clear and we get to the end then we can see there you go we now have an empty dictionary again we've gone back to it so there's a lot of things that are um oh i guess one more i want to do is i can do if uh let's do the clear after and actually let's get rid of that let's move that if up and so let's do well let's do it this way i'll go ahead and do all that and i'm going to go rebuild this thing so we come back i'm going to reset sample oh i can do before i do that all the way up in here i can do a sample 2 equals sample dot i can do a copy we've seen that one before so now after i clear sample i can do print sample two and then i can do if uh let's see let's do sample two let me go back to that let's see home and we're just going to take prints uh that key exists and so we can see through here that we cleared it out but now we have sample two that's just from that copy and we can see if that key exists if we do if sample two blue else that key does not exist and we can see here that again we're going to get it because it's uh it's checking there we can't do it exists on that so instead we'd have to to sample two dot get and because it's going to give us an empty then we can see that that key does not exist you three two one one more uh actually a couple more let's do uh since we've seen it not sample but we're going to do it on uh sample two you can also do a length which we have seen before and so if we jump out to that uh oh it says it does not have attribute of length oh sorry it's probably this that's where i got to do it i forgot so if i do it that way then we're going to see that it gives us the number of items within there and uh let's see i think that probably that's those are the things we're going to see in the most likely certification as we're going through that test there are a lot of similarities and they should make a lot of sense once you're playing around with lists and dictionaries as far as how it handles items and sizes and updates and displays and things like that probably key one to remember with a dictionary is although you can somewhat treat it as we've seen even the tuples the items you can treat them as an array it is safest to use your gets so that you can make sure that stuff doesn't blow up and then that's going to actually give you something that's a little more we'll say coding friendly that being said i think it's a good time to wrap this one up we will continue we're just going to keep chugging away at this thing there will be some additional examples and such in the notes in the show notes there's some links to some a couple other places where you can see how these things are used and as always we will have stuff out in the github repository so you can see this code and check it out and play around with it as at your leisure but as always go out there have yourself a great day a great week and we will talk to you next time
Transcript Segments
one well hello and welcome back
we're continuing our series of episodes
looking at python certification
this episode we're going to look at
dictionaries
we've not really i don't think we've
touched on these yet if you're
doing this in order so we're going to
get sort of step back and go to very
basic stuff talking about dictionaries
very similar to well in some ways
similar to lists
but there's also some differences now
we're going to work with this one
i've got a sample dictionary and this is
how you create one
essentially a fully populated dictionary
you're going to have a key and you're
going to have a value and then you use
comma to separate
here's another key another value another
key
number of values now let's start with
the most
basic
and let me do just so when you have that
dictionary
and print it out
then you'll see that it prints it out
almost identical to what it looks like
in the code so there's our keys and our
values now another thing we can do
is we can
we can actually print out the keys and
print out the
values so if we do that we see here
here's the diction and when we do notice
when we do
keys it actually says dictionary keys
it's going to print that out
so you have home office and bill those
are the three
keys that i created and then if you do
dictionary values it does same thing as
it tells you that's the values
and here are the ones that are within
that
so that's how you that's the uh the
usefulness basically the dictionary
is it's just a bunch of key value pairs
and there's plenty of opportunities for
us to use that in just about any
solution we run into
now once you if you want to work with
specific items there's actually two ways
to do it
you can uh let's do this
let's do that and so the two ways we can
do it
we can either look at it by key and
treat almost like an array
where the key is index or we can do a
get
now one of the things we're going to
find out is let's say
we'll follow that and let's see if we do
something that does not exist let's call
it red
and then see what happens in those cases
so in these two cases first two cases
we should get home which is one two
three main street
let's see what happens when we get when
we try to do uh
red
ah exercise so here we go so we got one
two three main street we get one two
three main street
however when we do it blows up right
here when we try to do
sample red then it gives us
an error so we can't do it uh if it
doesn't exist
because it's going to give an error
which is
it's a key error let's just keep the
same thing and do a get let's see what
it does here
notice here when we do the get
we don't get an error in that case it
figures out that hey that doesn't exist
so i'm going to send you a none i'm not
going to actually throw an error
i'm going to allow you to do it but i'm
going to set none back that basically
says i can't find a value for that
so generally speaking we're going to
want to use get
over this this other
construct basically to retrieve items
now we can do some things so we could do
like four um
assume value in
sample.values
actually let's do this let's do four key
and sample dot keys
and then we can do print let's see
key plus address
is and then we can do plus
um
sample dot get key
uh just get it close that so we can
we can do some of our some things we've
seen before
to work within these so we can see home
address is this and so this is our key
we do some stuff and that's our value
that's often how you're going to
walk through a dictionary is probably
going to do like you know four value in
either going to go through the values or
through the keys but of course if you do
the keys
then you can very easily do a get to get
a specific
value now another thing you can do is uh
let's see let's just print sample
you can use those keys to change them so
they are not immutable so if i do
sample let's see
home and then let's make it
uh let's just do it four three two
one whoops
some range which makes just have some
little random thing
and then we'll see that so now with this
one which is really where
we do need to use this uh this construct
to address stuff is we can actually
reassign it
so now if we look uh we can see here
so we come through we do our little loop
and then here we changed
out our value and assigned it to
something new
so we can use that to change things out
now we can we can
also do
let's do this
let's make this let's do vacation
this will be uh
sunshine avenue sounds like a nice
little
so now note we don't have that but we
can come in and take sample
and extend it so here we add a vacation
and it's just going to give us um
it just appends by default because key
order doesn't really matter but
it's going to pin that new key in so we
could add actually multiple things we
could create a dictionary
like let's do this
let's call it from blank
and we're just going to call blank
equals an empty dictionary
and then we can do blank um
let's just do it this way
a equals let's just do this
apple and then
uh print blank
and i'm gonna do it again and we'll see
that we can actually build something out
from scratch
like this
okay and then we'll see so then it goes
apple
let's see adds that in and it comes back
in and it adds banana
another thing we can do is uh this will
seem familiar if you've gone through the
lists
is that we can do a pop so we can do
let's do it off of sample
so we're going to do sample and we're
going to do it much like we did before
see
so if you do a dictionary pop
there's two of them in this case it's a
little different
normally you can either do you're going
to do it by key
so if you do a pop it's normally going
to need a key value
so i can specifically pull off let me go
back to
vacation
and let's see what comes out and then
see what sample looks like afterwards
so if we do that then
oh here we go so when we do the pop
it returns a value we pop much like we
saw in a list
and then also we can see now that it
pulled it out of the dictionary much
like just the same way it did list it
actually pulled that thing out of it
now if we do a pop item
and we'll just continue with that
then it's going to just pick something
so let's see what happens we're gonna do
a pop item twice
and let's see what it grabs
so we do it the first time it pulls
financial way which happens to be
the last item and then
and then it pulls it out and then the
second time we do a pop it's going to
pull the last item again
and it gives us just this one left so
much like um although
it may be a little tricky where the keys
are you don't necessarily know what the
order is
uh unless you you sort of know the value
with which you've added
the order with which you've added stuff
into the dictionary
but it's going to effectively work like
a pop and list where it's going to say
whatever is the last thing that came in
that's the first one i'm going to pull
out
so that's how you can you can do a pop
either a specific key or
you can do a pop item as we saw here
let's see um so we also did oh one we
did not look at
is items so let me go back to that
i forgot that one in there
because we looked at keys and we looked
at values and now let's look at items
which is another way
so we could easily do that
somewhere up here so we could do like
item in sample night item so let's go
see what items gives us because that's
another one we want to make sure we know
uh and as we can see here oops let me do
this beforehand
so let's move these pops
afterwards
so now you can see items
is when it sends it out it's going to
show you
it gives you the list so this is a comma
separated list
of the items much like you would see
uh like you see with the and it says
here it's items so you
you see each of those comma separated
values within the
the display uh now it's items it's a
keep
it's a key value pair so that also means
that if i were to take that
and just to make it a little more
blatant if i take this
loop
and i do item in
sample dot items
i'm just gonna do uh
let's do the next pair is
and it's just gonna do the item
uh let's get rid of that
and whoops
oh i cannot cat oh so i have to convert
that to a string which i think i can do
by this let's see if i can do that with
a tuple
can i i can okay so i convert the tuple
to a string
and um here we go next pair
so what we had here is we had lonesome
range office and billing
and you see that it pulls that whole
pair out
for each tuple and then we can do
something with it
and so let's say if we wanted to try to
just take
the first item let's just see what it
does there
because that's actually a good one to
note and so now if i just try to take
tread like an array and take the zero
based item
then um so it should go
next pair is and you'll see here where i
don't actually have to there's behind
the scenes
uh there is a an order
to it to some extent so i can
just take in this case the first item so
i'm taking the first key
here item 0 is going to be the
key for this item if i try to do item
one
then i'm saying i don't care what it is
in this
tuple so that's just zero and one zero
and one
and so it's going to take those and of
course if i try to push it
then it's going to give me issues
because uh here we go
where'd it go yeah tuple index out of
range
so there's only two in there so i could
work with it that way if i don't really
know
the key or the value then i could just
treat them as item zero and item one
and go from there but typically you're
going to send that out as some sort of a
tuple and then
and work with it that way so items gives
you
keys gives you the keys the left side of
the equation values gives you
the right side of the equation and then
items gives you
both it gives you each tuple
the two items that you see in the
dictionary so yes basically between each
comma so that's my tuple key value
here i have office in 22 baker street
office the key 22 baker streets of value
so on so i have keys values and then i
can do
items if i want to look at them that way
let's see another thing we can do that
we have seen before
is we can do a clear
let's do it this way let's
go ahead and clear it and then print it
so if we do a clear and we get to the
end then we can see
there you go we now have an empty
dictionary again we've gone back to it
so there's a lot of things that are um
oh i guess one more i want to do is i
can do if
uh let's do the clear after
and actually let's get rid of that let's
move that if up and so let's
do well let's do it this way i'll go
ahead and do all that and i'm going to
go rebuild this thing
so we come back i'm going to reset
sample
oh i can do before i do that
all the way up in here i can do
a sample 2 equals sample
dot i can do a copy we've seen that one
before
so now after i clear sample i can do
print sample two
and then i can do if
uh let's see let's do
sample two
let me go back to that let's see
home
and we're just going to take prints uh
that key exists
and so we can see through here that we
cleared it
out but now we have sample two that's
just from that copy
and we can see if that key exists if we
do if sample two
blue
else
that key does not exist
and we can see here that again
we're going to get it because it's uh
it's checking there we can't do it
exists on that so instead we'd have to
to
sample two dot get
and because it's going to give us
an empty then we can see that that key
does not exist
you
three two one one more
uh actually a couple more let's do uh
since we've seen it not sample but we're
going to do it on
uh sample two you can also do
a length which we have seen before
and so if we jump out to that
uh oh it says it does not have
attribute of length
oh sorry it's probably this
that's where i got to do it i forgot so
if i do it that way
then we're going to see that it gives us
the number of items within there
and uh let's see i think that probably
that's those are the things we're going
to see in the
most likely certification as we're going
through that test
there are a lot of similarities
and they should make a lot of sense once
you're playing around with lists and
dictionaries as far as how it handles
items and sizes and updates and displays
and things like that
probably key one to remember with a
dictionary is although you can
somewhat treat it as we've seen even the
tuples the items you can treat them
as an array it is safest
to use your gets so that you can make
sure that stuff doesn't blow up and then
that's going to actually give you
something that's a little more
we'll say coding friendly that being
said
i think it's a good time to wrap this
one up we will continue
we're just going to keep chugging away
at this thing there will be some
additional
examples and such in the notes in the
show notes there's some links to some
a couple other places where you can see
how these things are used and as always
we will have stuff out in the github
repository so you can see this code
and check it out and play around with it
as at your leisure
but as always go out there have yourself
a great day a great week
and we will talk to you next time