Dictionaries |Section 1|Celestial Warrior

1 year ago
18

1

00:00:00,709 --> 00:00:08,580

Hey, hello! Let's look at dictionaries in

this lecture. This far we've looked at strings

3

00:00:08,580 --> 00:00:15,150

we've looked at lists and tuples and now we

have yet another data type and these are

5

00:00:15,150 --> 00:00:23,430

dictionaries. Let me open the python shell.

And let me create a list first I'm just

7

00:00:23,430 --> 00:00:28,140

creating this for reference, so to

compare lists with dictionaries, but you

9

00:00:28,140 --> 00:00:32,940

don't need to create a list for creating

a dictionary. So to create a dictionary

11

00:00:32,940 --> 00:00:40,140

you need curly brackets, so open and

closing curly brackets and then inside

13

00:00:40,140 --> 00:00:45,719

that you'd pass the items of the dictionary.

Now the difference here with

15

00:00:45,719 --> 00:00:52,230

dictionaries is that instead of letting

Python add default indexes for your

17

00:00:52,230 --> 00:01:00,390

items you want to create those indexes

yourself exclusively so therefore for

19

00:01:00,390 --> 00:01:09,960

dictionaries you do something like this.

Name, a column and then the item. Let me

21

00:01:09,960 --> 00:01:20,159

write another one, surname column and

then the other item and yeah that's it.

23

00:01:20,159 --> 00:01:25,950

That's the dictionary. So what we have

here is when I was talking about

25

00:01:25,950 --> 00:01:31,229

indexing I was referring to these things.

Actually these two are referred to as

27

00:01:31,229 --> 00:01:38,189

keys, not indexes so for dictionaries you

can create custom indexes so to say and

29

00:01:38,189 --> 00:01:45,329

so these are keys so we have key and

value, key and value. This is good because

31

00:01:45,329 --> 00:01:51,329

now if you want to access one of the

values of the dictionary again you'd use

33

00:01:51,329 --> 00:01:56,939

square brackets just as you did with

strings lists and tuples and then there you

35

00:01:56,939 --> 00:02:02,909

pass the key which value you want to

extract, so if you want to extract John,

37

00:02:02,909 --> 00:02:09,300

you want to pass the key of John and

that will extract the John value. Something

39

00:02:09,300 --> 00:02:13,470

to note is that dictionaries are

an unordered colecton

41

00:02:13,470 --> 00:02:20,250

of items and so sometimes you may

notice that the order of the key and

43

00:02:20,250 --> 00:02:26,550

values is not maintained so you may see

that surname has gone to the first place, so

45

00:02:26,550 --> 00:02:32,160

surname Smith and the pair has gone the

first one and name John is the second

47

00:02:32,160 --> 00:02:37,110

one and so on. So this is different from

lists, strings and tuples. The reason why

49

00:02:37,110 --> 00:02:42,210

Python doesn't keep the order is that

you know the way you access dictionaries

51

00:02:42,210 --> 00:02:49,890

is you exclusively pass the key of the

dictionary when you access it and so you

53

00:02:49,890 --> 00:02:54,209

don't need to know the order of the dictionary.

And whenever you want to access

55

00:02:54,209 --> 00:02:57,510

something you say the surname for

instance and that will the return Smith

57

00:02:57,510 --> 00:03:02,340

no matter where Smith is, so whether it

is the first one, the second one or over

59

00:03:02,340 --> 00:03:10,500

and so on. And interestingly in

dictionaries you can also pass, you can

61

00:03:10,500 --> 00:03:17,940

have like lists as values or even tuples,

so let me write a circle there or let me

63

00:03:17,940 --> 00:03:27,000

keep it like that and let me add another

key and pair here. Let's say cities. Let's say

65

00:03:27,000 --> 00:03:42,750

Joe Smith lives in three cities, Porto

and San Diego and Bali. You close

67

00:03:42,750 --> 00:03:49,769

bracket there so as you see we have the

key here, we have a column and then you

69

00:03:49,769 --> 00:03:54,030

have the value which can be anything so

it can be string, it can be a number, it

71

00:03:54,030 --> 00:04:01,100

can be at list, a tuple. In this case

that's a tuple so if you access now

73

00:04:01,100 --> 00:04:10,070

cities like that, you'd get… Sorry, I'll add

two bracket there, so you'd get the tuple.

75

00:04:10,070 --> 00:04:15,959

And then you can also do tricks like you

know if you want to access Bali you'd

77

00:04:15,959 --> 00:04:23,789

have other brackets there and pass 2.

And that would return Bali, so why is that?

79

00:04:23,789 --> 00:04:27,710

That's because you know up to here you

have a tuple,

81

00:04:27,710 --> 00:04:35,849

but that's a triple object and

then from that tuple you use again the

83

00:04:35,849 --> 00:04:40,560

square brackets notation to access an

item from that tuple, so you want item

85

00:04:40,560 --> 00:04:47,759

with index two and that is Bali and yeah

that's about dictionaries. Try them out

87

00:04:47,759 --> 00:04:55,110

some more, play around and you will be able

to comfortably create and access items

89

00:04:55,110 --> 00:05:01,099

from a dictionary. So I hope you enjoyed

this and talk to you later.

Loading comments...