Python for Beginners, Part 16: Dictionaries by@cleverprogrammer

Python for Beginners, Part 16: Dictionaries

Clever Programmer HackerNoon profile picture

Clever Programmer

Clever Programmer is a community with over 100,000+ students who are learning to code by building real world projects.

Let's talk about yet another fascinating data structure known as dictionaries. Ever wondered how your iPhone stores phone numbers and then allows for simple and efficient lookups? Well, no need to wonder anymore after you watch this video!

In case you missed it, here are the previous parts of the series:


0:00 hey guys how's it going so in the last

0:02 video we talked about doing projects

0:06 right so we going over the solutions of

0:09 some projects and this video what I talk

0:11 about is dictionaries. They're very

0:13 important in a lot of programming

0:16 languages and other ones and in C/C++ it

0:19 might be called maps but in Python it's

0:21 called a dictionary so let's see what it

0:24 is about. Okay so essentially the idea

0:29 behind dictionaries is to map keys to

0:34 values

0:36 hopefully that defines everything right

0:38 now

0:40 basically that's a very cryptic

0:42 definition that I gave you if you don't

0:43 know what it is yeah it maps two things

0:48 okay so anytime you have a relationship

0:50 between two things you want to use

0:52 dictionaries and when you don't have a

0:54 relationship relationship between two

0:57 things you want to use a list for

0:59 example if you want to just remember a

1:02 bunch of numbers and that's all you care

1:04 about you want to use a list okay so for

1:07 example I only care about a bunch of

1:10 numbers like this I want to use a list

1:15 now what if I want to know a way where

1:19 hmm

1:20 I don't know I use my cell phone right I

1:24 use this thing over here and using this

1:28 thing I can look up a person's name and

1:33 based on that person's name it gives me

1:35 back their phone number right or if I

1:38 type in their phone number it kind of

1:40 goes the other way around

1:41 whatever how can we do that well that's

1:45 the beauty of dictionaries my friend so

1:48 you're going to learn something very

1:50 powerful today so let's let's go at it

1:53 let's say I wanted to make a phone book

1:56 write with what we know in list I mean

2:00 can you think of a way how you might do

2:02 it I don't know I mean you could store

2:08 numbers right like this as a string and

2:12 then store the second number as a string

2:15 and go 4 5 6 2 2 4 4 3 3 right something

2:20 like that whatever these are not clearly

2:22 very silly numbers but ok and then you

2:25 can say phone book and give me the

2:26 zeroth number phone book give me the

2:28 first number also notice we're using

2:30 something different for coding we're

2:32 using rappelled IT right so it's

2:35 actually spelled repl by T and the

2:40 beauty of this is that it teaches you

2:43 how to code while it makes it very easy

2:45 for you to code you can use this without

2:48 having an actual working environment

2:52 locally downloading onto your computer

2:54 you can just run your code on the fly

2:55 and what I'm going to do for you guys

2:57 is once I'm done with this session I'm

3:00 going to take this link and paste it

3:02 into the YouTube description and that

3:05 way you can actually get access to these

3:07 notes directly ok so you'll be able to

3:10 come to this link and get for example

3:14 the link right now is this ok by it but

3:18 by the time we're done with this video

3:19 the link might be different and I'll

3:21 post the most updated link for you guys

3:22 in the YouTube description ok so the

3:27 website is repl DT and you can pick

3:30 programming language Python for your

3:33 programming language and then just code

3:35 here ok

3:36 so let's check out what this does let's

3:38 make sure to add print print let's check

3:44 it out

3:44 so you can see you have 1 2 3 & 4 5 6

3:47 right you printing out the zeroth

3:49 element and then the first element of

3:51 this list that's great but I mean it's

3:54 not really fun to remember names as

3:56 numbers right because you're not going

3:58 to be you're not gonna go oh I'm gonna

4:01 call you zero Johnny and I'm going to

4:03 call you 1 Bob and then every time you

4:05 have to look at Johnny's number you're

4:07 going to type in zero to get back

4:08 Johnny's number or one to get Bob's

4:10 number that would be terrible

4:13 and what if you had stored hundreds of

4:16 people in your list then it gets really

4:18 complicated but now let's use

4:20 dictionaries here's how dictionaries

4:22 work okay let me describe right here in

4:25 notes dictionaries okay

4:31 here's other word you have some

4:34 dictionary we will called it a dick okay

4:38 and you did and the store key value

4:42 pairs so if you put in a key gives you

4:44 back the value okay and how you store

4:49 things into a dictionary is like so your

4:52 key goes here and your value goes here

4:54 okay let's set a key one value 1 then we

4:58 can say key - value 2 okay and you can

5:03 keep doing this all day all right now

5:07 let's create a dictionary

5:09 ok so let's create our example phonebook

5:12 so I'm going to say

5:14 Kazi which is my name which is my last

5:17 name but I got go on my last name pretty

5:19 much everywhere so we're going to take

5:20 the string cause E as the cause e is the

5:24 what is that the key or the value if you

5:28 said key good job and we're going to

5:30 store the number 1 2 3 4 5 6 7 8 9 0 now

5:37 notice that I'm storing this as a string

5:40 and not an actual number like an integer

5:43 or something why do you think that is

5:45 well think about it if I remove the

5:48 quotes from here what is this going to

5:50 do is going to do 1 2 3 - 4 5 6 - 7 8 9

5:56 0 and I'm going to end up getting back

5:59 on negative 8223 so it's actually going

6:03 to end up storing it like this that's

6:05 not what I want I want the exact number

6:08 so usually when you want things exactly

6:10 how they are strings are perfectly good

6:12 data type to use there and now I'm going

6:15 to say Bob and I'm going to store Bob's

6:17 number as okay is this a which key is

6:22 this this is the second key and now

6:24 there's going to be the second

6:25 I'm going to store his number as 2 2 2 2

6:27 2 2 & 2 2 2 2

6:29 very easy number that Bob has he gets

6:32 tons of wrong calls and he gets very

6:37 annoyed now to make it a little bit

6:39 nicer on me to read this dictionary sure

6:41 I can have it as a long one line but as

6:43 you can imagine as we begin to store

6:46 multiple people it's going to be

6:47 spilling over to the right side so what

6:49 I want to do is make it easier on my

6:51 eyes and make it easier to type so I'm

6:55 going to go to the start of the line hit

6:56 enter and python knows what to do with

6:58 it and where to put it and I'm going to

7:00 go to the start of this line and hit

7:01 enter okay I'm also going to go here and

7:04 hit enter and I'm going to go here and

7:07 hit enter okay

7:08 and what this does for me is it just

7:11 makes it easier for me to read okay kind

7:14 of like this um and i can just simply

7:19 start putting things in here we can

7:21 leave that here there that's not a

7:22 problem we can leave that one right up

7:24 there and i can put in another person

7:27 right cat cat cat

7:29 let's just do cat caller cat let's say

7:32 her number is 3 3 3 3 3 3 3 ok and now

7:37 what I can do is I can do phone book and

7:39 what's the way I can get back cause ease

7:42 phone number right so imagine you go to

7:46 your phone your iPhone or whatever you

7:48 have and you simply swipe up or down

7:52 right you go to your words contacts how

7:57 does contacts work I go to my contacts

8:01 and I type in let's say my name right so

8:06 how does it find it and how does it give

8:09 me back the actual number well today

8:11 you're going to find out the secret

8:13 behind how it works ok so it takes in

8:18 the key or my name Rafeh Qazi and it

8:20 spits back the value associated with it

8:23 it could be my email address it could be

8:25 my phone number it could be whatever the

8:27 heck I want it to be right so now let's

8:29 go into phonebook and we can say Qazi

8:32 right that's you put in the key and this

8:35 whole thing liquefies and

8:38 turns into the value okay so that whole

8:41 thing turns into one value okay so let's

8:45 print it out and let's see what we get

8:47 and I'll break it down how this

8:48 expression works so we got back one two

8:52 three four five six seven eight nine

8:54 zero phone book is just this whole

8:56 dictionary starting with the squiggly is

8:58 right so phone book is this thing right

9:00 there

9:01 and I'm indexing that phone book by Qazi

9:05 okay so as you can see I have this list

9:08 here and I'm simply indexing it by qaz I

9:12 okay

9:13 and once I index it what happens here

9:17 right once I index it by Qazi I just get

9:20 back the value so this whole thing

9:23 liquidates into that and then that

9:27 result actually gets printed out on the

9:29 screen okay that's how the dictionary

9:33 works now let's say I wanted to make it

9:35 a little bit more complicated to where

9:38 each person had you know like their

9:43 names and each person had their own

9:45 email address associated with them and

9:48 whatever right so we could make it a

9:50 little bit more interesting for example

9:52 let's say that I wanted to add more

9:55 values associated with Qazi I don't want

9:57 just his phone number but kind of like

9:59 how my phone does it

10:00 it also has caused these email addresses

10:03 and it also has causes home address and

10:06 whatever else right so how would I do

10:07 that well what I need to do is I need to

10:10 have one key that gives me back multiple

10:13 values associated with it here I have a

10:15 key Qazi I have a key Bob and I have a

10:18 key tag so what I need to do to cause e

10:21 for example is make it give me back

10:24 multiple values well what's a pretty

10:26 darn good thing to give back multiple

10:28 things hmmm I remember it's a list how

10:34 about a list of things so let's turn

10:37 this from a spare string to a list that

10:40 contains one the phone number and then

10:44 we can say to it contains one email

10:47 address so we can say

10:50 Ozzie at cause okay fake email

10:54 address let's close that down there and

11:00 okay sweet so now we have a list

11:02 containing two strings inside of it the

11:05 first at first index it's a phone number

11:08 and then order 0th index rather and then

11:11 at the first index we have two email

11:12 address I can do the same thing to the

11:16 other ones but let's just play around

11:18 with it and see what we get so I'm going

11:21 to change this from phonebook Aussie and

11:23 I'm going to say ok I know that once I

11:27 run this line of code what I will get

11:30 back is actually this list this whole

11:34 thing is actually going to evaluate to

11:35 the value of Kazi right so you put in

11:38 the key look at the formula at the top

11:39 right you put in the key and you get

11:41 back the value from the other side so

11:44 I'm going to run it and let's see what

11:45 we get so you can see that we get back

11:47 the value I know that this thing is

11:50 going to turn into a list then I have to

11:53 think about how can I access different

11:55 things from the list let's say my

11:57 question to you was can you get me cause

11:59 these phone number you would say okay

12:02 I'll get you cause these phone number I

12:04 know that it's inside of a dictionary

12:06 where the key is

12:07 Kazi where there is a key cause II and

12:10 if I access that key Kazi right if I

12:14 access the key the dict and if I access

12:19 the key Kazi I get back a list of things

12:24 containing his a phone number and email

12:31 right so you're like boom now I know

12:34 that I have a list of things how can I

12:37 access things inside of a list I

12:38 remember how you access things inside of

12:42 a list is based on the index number

12:44 right so for example if I go up here if

12:48 I have to access the first element or

12:51 the zeroth element of the list I would

12:52 do this and this gives me the first

12:54 element

12:54 list whereas for a dictionary you would

12:58 actually put in for a dictionary you

13:03 would actually put in a name or

13:05 something something that's nice and easy

13:07 to remember in a list you use numbers so

13:11 then you go okay

13:12 well I know that cause ease email

13:15 address is at position one right this is

13:19 index zero and this is index one so if I

13:21 ask you the question of getting me cause

13:23 ease email address you know that this

13:26 whole expression evaluates to a list

13:28 well then you can just say one right

13:33 just like this thing over here and let's

13:37 change it to zeroth element the list

13:39 just to be very accurate let's hit that

13:41 and you can see that I get back Ozzy's

13:43 email address well what if I wanted to

13:46 get a cause ease phone number I would

13:50 just change this to a zero and you can

13:53 see that I got back that's the same

13:54 thing now let's say I want to do the

13:57 same thing for Bob right turn this is

13:59 the phone number this is Bob at Bob calm

14:01 and then we have let's turn this into a

14:05 list of things and let's call it cat at

14:09 cat calm and we have this closing I have

14:13 to make sure that I close my quotes here

14:17 close the quotes there okay great so I

14:21 have a phone book let's get the

14:24 terminology right let's just practice

14:25 with the terminology it's good to say it

14:28 over and over again I don't care if I

14:30 sound crazy and I you shouldn't care the

14:32 few sound crazy but understand the

14:34 terminology because is going to help you

14:35 be able to research it better understand

14:37 it better synthesize the information

14:39 better and be able to write better code

14:42 ultimately because you will understand

14:43 all of the data structures whereas if

14:46 you just keep mumble and hand-waving

14:48 you're going to get very confused later

14:50 on you might be able to get past some of

14:51 this stuff but then when we get into

14:53 list inside of dictionaries inside lists

14:55 of dictionaries is going to get very

14:57 complicated you can be like oh man I

14:59 wish I learned the terminology when it

15:01 was nice and easy so let's let's just

15:04 talk through it I have a dictionary that

15:06 contains how many keys

15:08 three keys what's each keys name Kazi

15:11 Bob cat what do each of the keys contain

15:15 each of the key contains a value and

15:17 that value is a list okay good each key

15:22 contains a value which is a list that's

15:24 good and how many elements is each list

15:26 contain each list contains two elements

15:29 right as we can see right here so now if

15:34 I wanted to get cats number I could do

15:38 cat and I can say zero and I get back

15:43 three three three three three three

15:45 three three three three so let's give it

15:46 an exact let's give it a run and you can

15:48 see on the right-hand side that's what I

15:50 get back okay so I will leave this here

15:54 for you guys and I will link you guys to

15:56 the code so you guys can go and play

15:59 around with this stuff as well okay

16:04 that's it for dictionaries and we will

16:07 get into its better use cases later on

16:09 for now you're gonna watch Boolean

16:13 algebra jujitsu after this video great

16:17 job guys take care I will see you in the

16:21 next video


Signup or Login to Join the Discussion


Related Stories