📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Dictionaries

2021-07-27 •Youtube

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
0.64

one well hello and welcome back

3.04

we're continuing our series of episodes

5.759

looking at python certification

8.32

this episode we're going to look at

10.96

dictionaries

12.639

we've not really i don't think we've

14.16

touched on these yet if you're

15.679

doing this in order so we're going to

17.44

get sort of step back and go to very

20

basic stuff talking about dictionaries

23.519

very similar to well in some ways

25.76

similar to lists

27.119

but there's also some differences now

29.92

we're going to work with this one

31.279

i've got a sample dictionary and this is

34.719

how you create one

36.32

essentially a fully populated dictionary

39.84

you're going to have a key and you're

41.2

going to have a value and then you use

43.04

comma to separate

44.32

here's another key another value another

46.879

key

47.36

number of values now let's start with

50.399

the most

51.44

basic

56

and let me do just so when you have that

58.8

dictionary

59.52

and print it out

62.719

then you'll see that it prints it out

64.08

almost identical to what it looks like

65.76

in the code so there's our keys and our

67.439

values now another thing we can do

70

is we can

74.159

we can actually print out the keys and

76.56

print out the

77.439

values so if we do that we see here

81.439

here's the diction and when we do notice

83.119

when we do

83.68

keys it actually says dictionary keys

86.32

it's going to print that out

87.6

so you have home office and bill those

89.52

are the three

91.04

keys that i created and then if you do

93.119

dictionary values it does same thing as

94.72

it tells you that's the values

96.32

and here are the ones that are within

98.84

that

100.4

so that's how you that's the uh the

102.64

usefulness basically the dictionary

104.72

is it's just a bunch of key value pairs

107.119

and there's plenty of opportunities for

109.439

us to use that in just about any

111.36

solution we run into

113.28

now once you if you want to work with

114.64

specific items there's actually two ways

116.64

to do it

117.84

you can uh let's do this

132.319

let's do that and so the two ways we can

135.28

do it

136.879

we can either look at it by key and

139.12

treat almost like an array

141.04

where the key is index or we can do a

143.92

get

145.28

now one of the things we're going to

146.8

find out is let's say

148.48

we'll follow that and let's see if we do

150

something that does not exist let's call

151.76

it red

154.64

and then see what happens in those cases

156.16

so in these two cases first two cases

158.239

we should get home which is one two

160.16

three main street

161.36

let's see what happens when we get when

163.44

we try to do uh

164.72

red

167.76

ah exercise so here we go so we got one

169.44

two three main street we get one two

171.28

three main street

172.239

however when we do it blows up right

175.92

here when we try to do

177.12

sample red then it gives us

180.239

an error so we can't do it uh if it

183.28

doesn't exist

184.4

because it's going to give an error

185.68

which is

187.599

it's a key error let's just keep the

190.08

same thing and do a get let's see what

191.36

it does here

192.64

notice here when we do the get

195.68

we don't get an error in that case it

199.04

figures out that hey that doesn't exist

200.8

so i'm going to send you a none i'm not

202.48

going to actually throw an error

204

i'm going to allow you to do it but i'm

205.12

going to set none back that basically

206.56

says i can't find a value for that

209.12

so generally speaking we're going to

211.28

want to use get

212.48

over this this other

216.64

construct basically to retrieve items

220.64

now we can do some things so we could do

222.159

like four um

226

assume value in

231.4

sample.values

236.72

actually let's do this let's do four key

241.68

and sample dot keys

246.879

and then we can do print let's see

254.56

key plus address

262.32

is and then we can do plus

267.6

um

273.04

sample dot get key

279.28

uh just get it close that so we can

283.199

we can do some of our some things we've

285.199

seen before

286.96

to work within these so we can see home

289.36

address is this and so this is our key

291.44

we do some stuff and that's our value

293.68

that's often how you're going to

295.6

walk through a dictionary is probably

297.28

going to do like you know four value in

299.12

either going to go through the values or

300.479

through the keys but of course if you do

302.8

the keys

303.44

then you can very easily do a get to get

306.72

a specific

307.759

value now another thing you can do is uh

311.039

let's see let's just print sample

314.16

you can use those keys to change them so

316.24

they are not immutable so if i do

318.56

sample let's see

322.84

home and then let's make it

326

uh let's just do it four three two

329.28

one whoops

334.88

some range which makes just have some

337.6

little random thing

338.88

and then we'll see that so now with this

341.68

one which is really where

343.199

we do need to use this uh this construct

345.759

to address stuff is we can actually

347.039

reassign it

347.759

so now if we look uh we can see here

352.24

so we come through we do our little loop

354.16

and then here we changed

356.4

out our value and assigned it to

358.88

something new

360.72

so we can use that to change things out

363.28

now we can we can

364.479

also do

369.12

let's do this

372.4

let's make this let's do vacation

377.199

this will be uh

384.16

sunshine avenue sounds like a nice

386.639

little

390.56

so now note we don't have that but we

392.319

can come in and take sample

394.16

and extend it so here we add a vacation

398.24

and it's just going to give us um

401.28

it just appends by default because key

403.84

order doesn't really matter but

405.759

it's going to pin that new key in so we

407.36

could add actually multiple things we

409.039

could create a dictionary

411.84

like let's do this

419.759

let's call it from blank

424.319

and we're just going to call blank

426.24

equals an empty dictionary

429.28

and then we can do blank um

432.319

let's just do it this way

435.84

a equals let's just do this

441.36

apple and then

447.52

uh print blank

453.599

and i'm gonna do it again and we'll see

454.96

that we can actually build something out

456.72

from scratch

457.84

like this

463.039

okay and then we'll see so then it goes

465.919

apple

466.4

let's see adds that in and it comes back

468.08

in and it adds banana

470.479

another thing we can do is uh this will

473.28

seem familiar if you've gone through the

474.72

lists

475.759

is that we can do a pop so we can do

479.36

let's do it off of sample

484.24

so we're going to do sample and we're

485.68

going to do it much like we did before

490.84

see

495.52

so if you do a dictionary pop

503.68

there's two of them in this case it's a

506

little different

506.96

normally you can either do you're going

509.84

to do it by key

511.84

so if you do a pop it's normally going

513.44

to need a key value

515.76

so i can specifically pull off let me go

518.24

back to

518.839

vacation

522

and let's see what comes out and then

523.36

see what sample looks like afterwards

526.32

so if we do that then

531.279

oh here we go so when we do the pop

534.8

it returns a value we pop much like we

536.64

saw in a list

538.32

and then also we can see now that it

540.64

pulled it out of the dictionary much

542.08

like just the same way it did list it

543.519

actually pulled that thing out of it

545.36

now if we do a pop item

548.8

and we'll just continue with that

558.08

then it's going to just pick something

560.24

so let's see what happens we're gonna do

561.6

a pop item twice

563.76

and let's see what it grabs

569.2

so we do it the first time it pulls

570.959

financial way which happens to be

572.8

the last item and then

576.959

and then it pulls it out and then the

578.64

second time we do a pop it's going to

580.08

pull the last item again

582.8

and it gives us just this one left so

585.839

much like um although

589.12

it may be a little tricky where the keys

590.959

are you don't necessarily know what the

592.08

order is

593.519

uh unless you you sort of know the value

596.16

with which you've added

597.36

the order with which you've added stuff

599.279

into the dictionary

601.839

but it's going to effectively work like

604.64

a pop and list where it's going to say

606.48

whatever is the last thing that came in

608.16

that's the first one i'm going to pull

609.44

out

610.32

so that's how you can you can do a pop

613.36

either a specific key or

616.72

you can do a pop item as we saw here

622.88

let's see um so we also did oh one we

627.36

did not look at

628.32

is items so let me go back to that

632

i forgot that one in there

638.24

because we looked at keys and we looked

640

at values and now let's look at items

642.88

which is another way

646.48

so we could easily do that

649.839

somewhere up here so we could do like

651.519

item in sample night item so let's go

653.44

see what items gives us because that's

655.12

another one we want to make sure we know

657.36

uh and as we can see here oops let me do

660.72

this beforehand

662

so let's move these pops

666.72

afterwards

672.64

so now you can see items

675.839

is when it sends it out it's going to

678.56

show you

679.12

it gives you the list so this is a comma

681.6

separated list

682.48

of the items much like you would see

686.079

uh like you see with the and it says

688.24

here it's items so you

689.839

you see each of those comma separated

691.92

values within the

693.36

the display uh now it's items it's a

696.16

keep

696.48

it's a key value pair so that also means

699.6

that if i were to take that

701.04

and just to make it a little more

702.48

blatant if i take this

705.519

loop

708.72

and i do item in

712.16

sample dot items

716.079

i'm just gonna do uh

721.76

let's do the next pair is

725.76

and it's just gonna do the item

729.36

uh let's get rid of that

733.92

and whoops

738.959

oh i cannot cat oh so i have to convert

740.959

that to a string which i think i can do

743.44

by this let's see if i can do that with

744.959

a tuple

747.68

can i i can okay so i convert the tuple

750.8

to a string

752.48

and um here we go next pair

755.68

so what we had here is we had lonesome

757.519

range office and billing

759.68

and you see that it pulls that whole

761.6

pair out

763.2

for each tuple and then we can do

765.2

something with it

769.12

and so let's say if we wanted to try to

771.44

just take

774.56

the first item let's just see what it

776.32

does there

778.8

because that's actually a good one to

780.24

note and so now if i just try to take

782.959

tread like an array and take the zero

784.72

based item

786.8

then um so it should go

790.56

next pair is and you'll see here where i

793.839

don't actually have to there's behind

795.6

the scenes

796.88

uh there is a an order

800.16

to it to some extent so i can

803.2

just take in this case the first item so

805.36

i'm taking the first key

807.519

here item 0 is going to be the

812.839

key for this item if i try to do item

815.92

one

818.48

then i'm saying i don't care what it is

822.24

in this

822.8

tuple so that's just zero and one zero

825.36

and one

825.92

and so it's going to take those and of

827.76

course if i try to push it

829.519

then it's going to give me issues

831.04

because uh here we go

834.079

where'd it go yeah tuple index out of

836.24

range

837.6

so there's only two in there so i could

840

work with it that way if i don't really

841.519

know

842.88

the key or the value then i could just

844.639

treat them as item zero and item one

847.36

and go from there but typically you're

849.6

going to send that out as some sort of a

850.88

tuple and then

851.68

and work with it that way so items gives

854.32

you

855.04

keys gives you the keys the left side of

856.8

the equation values gives you

858.959

the right side of the equation and then

861.839

items gives you

862.959

both it gives you each tuple

866.24

the two items that you see in the

869.92

dictionary so yes basically between each

872.32

comma so that's my tuple key value

874.88

here i have office in 22 baker street

878.16

office the key 22 baker streets of value

881.68

so on so i have keys values and then i

884.24

can do

884.8

items if i want to look at them that way

889.44

let's see another thing we can do that

891.12

we have seen before

895.76

is we can do a clear

904.56

let's do it this way let's

907.76

go ahead and clear it and then print it

914.399

so if we do a clear and we get to the

916.079

end then we can see

917.92

there you go we now have an empty

920

dictionary again we've gone back to it

924.32

so there's a lot of things that are um

929.519

oh i guess one more i want to do is i

931.12

can do if

934.88

uh let's do the clear after

942.56

and actually let's get rid of that let's

944

move that if up and so let's

945.92

do well let's do it this way i'll go

948.16

ahead and do all that and i'm going to

949.04

go rebuild this thing

950.8

so we come back i'm going to reset

952.839

sample

955.36

oh i can do before i do that

958.48

all the way up in here i can do

962.16

a sample 2 equals sample

965.44

dot i can do a copy we've seen that one

968.16

before

969.92

so now after i clear sample i can do

973.12

print sample two

977.36

and then i can do if

980.72

uh let's see let's do

984.24

sample two

987.44

let me go back to that let's see

990.56

home

995.279

and we're just going to take prints uh

1000.399

that key exists

1006.959

and so we can see through here that we

1009.519

cleared it

1010

out but now we have sample two that's

1011.839

just from that copy

1013.6

and we can see if that key exists if we

1016

do if sample two

1022.839

blue

1025.039

else

1030.24

that key does not exist

1037.199

and we can see here that again

1041.839

we're going to get it because it's uh

1043.679

it's checking there we can't do it

1044.959

exists on that so instead we'd have to

1048.72

to

1049.2

sample two dot get

1053.44

and because it's going to give us

1056.96

an empty then we can see that that key

1059.679

does not exist

1079.84

you

1105.36

three two one one more

1108.64

uh actually a couple more let's do uh

1111.44

since we've seen it not sample but we're

1113.12

going to do it on

1113.919

uh sample two you can also do

1117.919

a length which we have seen before

1121.679

and so if we jump out to that

1124.799

uh oh it says it does not have

1128

attribute of length

1132.799

oh sorry it's probably this

1137.6

that's where i got to do it i forgot so

1139.6

if i do it that way

1142.4

then we're going to see that it gives us

1143.679

the number of items within there

1148.48

and uh let's see i think that probably

1152

that's those are the things we're going

1154

to see in the

1155.679

most likely certification as we're going

1158.24

through that test

1159.6

there are a lot of similarities

1163.12

and they should make a lot of sense once

1166

you're playing around with lists and

1167.12

dictionaries as far as how it handles

1169.919

items and sizes and updates and displays

1172.72

and things like that

1173.919

probably key one to remember with a

1175.6

dictionary is although you can

1177.12

somewhat treat it as we've seen even the

1179.2

tuples the items you can treat them

1181.039

as an array it is safest

1184.64

to use your gets so that you can make

1186.64

sure that stuff doesn't blow up and then

1188.16

that's going to actually give you

1189.2

something that's a little more

1191.44

we'll say coding friendly that being

1194.4

said

1194.72

i think it's a good time to wrap this

1195.919

one up we will continue

1198.559

we're just going to keep chugging away

1199.76

at this thing there will be some

1201.52

additional

1202.4

examples and such in the notes in the

1204.32

show notes there's some links to some

1206.24

a couple other places where you can see

1207.76

how these things are used and as always

1209.84

we will have stuff out in the github

1211.12

repository so you can see this code

1213.28

and check it out and play around with it

1215.36

as at your leisure

1217.28

but as always go out there have yourself

1219.039

a great day a great week

1220.96

and we will talk to you next time