1
00:00:00,720 --> 00:00:00,970
Hello.

2
00:00:00,990 --> 00:00:01,650
Welcome back.

3
00:00:01,980 --> 00:00:08,010
So in this lesson we are going to look at dictionaries so dictionaries the data types similar to arrays

4
00:00:08,280 --> 00:00:13,170
but works with keys and values instead of indices we access to.

5
00:00:13,170 --> 00:00:21,090
I remember using the index of the member in a dictionary we access a particular member using the key

6
00:00:21,180 --> 00:00:23,750
of the off the dictionary.

7
00:00:23,790 --> 00:00:24,750
So let's get started.

8
00:00:24,750 --> 00:00:28,100
Let's create a new project went to click over here.

9
00:00:28,320 --> 00:00:33,930
What I'm going to do is every time I create a new project I have to reshape my window in order to reshape

10
00:00:33,930 --> 00:00:35,870
I'm going to use save us.

11
00:00:35,880 --> 00:00:39,390
And then just say no 8 over here dictionary.

12
00:00:40,490 --> 00:00:45,270
So then we have a new copy of this current project and then I'm just going to clean everything from

13
00:00:45,270 --> 00:00:46,050
here.

14
00:00:46,260 --> 00:00:47,570
We've got a new project.

15
00:00:47,580 --> 00:00:48,950
We can start with.

16
00:00:49,060 --> 00:00:57,330
So let's say we create a database that contains the name of the names of people and their ages.

17
00:00:57,330 --> 00:00:58,270
We can come here.

18
00:00:58,290 --> 00:01:01,100
Let's start by putting a simple description here.

19
00:01:01,170 --> 00:01:08,880
I'll put a comment out of call this example one and say creating a dictionary Hellboy they're creating

20
00:01:09,000 --> 00:01:12,320
a dictionary Dictionary.

21
00:01:12,810 --> 00:01:13,320
Right.

22
00:01:14,220 --> 00:01:20,580
And let's just say print because we're going to have a number of examples would be nice to have to know

23
00:01:20,580 --> 00:01:23,360
which is example one example to Exeter.

24
00:01:23,370 --> 00:01:31,840
So I'm just going to put a helper here print example one like this.

25
00:01:32,710 --> 00:01:33,140
OK.

26
00:01:33,240 --> 00:01:34,620
Now we're here.

27
00:01:34,620 --> 00:01:35,790
Let's get to our dictionary.

28
00:01:35,790 --> 00:01:42,060
So to create a dictionary we can just start by using a dictionary named let's say we want a dictionary

29
00:01:42,060 --> 00:01:47,850
to be called ages so I can say is of course this we've created a dictionary.

30
00:01:47,850 --> 00:01:52,880
Now how do we add the keys and values to the dictionary.

31
00:01:52,890 --> 00:01:54,040
Very simple.

32
00:01:54,040 --> 00:01:58,710
I'll come here dictionary name ages square brackets.

33
00:01:58,920 --> 00:02:02,420
I want to add a key known as Tom T or M.

34
00:02:02,530 --> 00:02:04,140
What's the value for Tom.

35
00:02:04,140 --> 00:02:12,460
Twenty nine and then I want to add another another another row to the database you can make.

36
00:02:12,690 --> 00:02:13,790
So it's key value.

37
00:02:13,800 --> 00:02:14,540
Key value.

38
00:02:14,550 --> 00:02:19,020
I didn't want to say key value because they go together by the represent a row.

39
00:02:19,230 --> 00:02:22,850
So let's say I want to add another person to the database.

40
00:02:22,890 --> 00:02:24,240
How about Dick.

41
00:02:24,330 --> 00:02:29,420
So I call this ages 0 use ages Does the name of the dictionary.

42
00:02:29,810 --> 00:02:38,600
And the key is stake and the value of Dick which in this case is his age is 30.

43
00:02:38,610 --> 00:02:42,440
And we can add a third person to get Harry.

44
00:02:42,600 --> 00:02:45,950
Come on Tom Dick and Harry.

45
00:02:46,050 --> 00:02:52,490
Harry over her and we can give Harry the youngest age he's 19.

46
00:02:52,680 --> 00:02:55,290
So let's say we want to print the dictionary.

47
00:02:55,290 --> 00:02:56,370
Print ages.

48
00:02:56,370 --> 00:03:01,150
Let's see what we get with press control us to save.

49
00:03:01,150 --> 00:03:07,680
Click here to run would click here to run module and asking to see it's printed keys and values over

50
00:03:07,680 --> 00:03:08,130
here.

51
00:03:08,130 --> 00:03:14,160
If you see this arrangement in a way in Python this is a dictionary printed it's got a key that value

52
00:03:14,160 --> 00:03:15,830
the key to value the key to value.

53
00:03:15,840 --> 00:03:16,320
Tom.

54
00:03:16,320 --> 00:03:19,270
Twenty nine Dick 30 Harry.

55
00:03:19,340 --> 00:03:21,220
Nineteen right.

56
00:03:21,240 --> 00:03:23,510
Very simple very straightforward.

57
00:03:23,550 --> 00:03:25,840
So this is how to create dictionary.

58
00:03:25,860 --> 00:03:30,910
So now let's use a different way of initialize in the dictionary.

59
00:03:31,590 --> 00:03:39,550
I'll come down here or call this example to I'll give it a different head in OK over here different

60
00:03:39,550 --> 00:03:47,880
way of initialization I'll call this different Wolf initialization and can call this print example to

61
00:03:47,880 --> 00:03:49,090
like this.

62
00:03:49,200 --> 00:03:59,040
So let's say we create an a database of postcodes we use a particular area in this city and then we

63
00:03:59,040 --> 00:04:05,250
use the area and I suggest the key and then the postcode as the value to let's say postcodes.

64
00:04:05,280 --> 00:04:06,810
That's the name of the dictionary.

65
00:04:06,810 --> 00:04:13,110
And this time rather than create the dictionary and add its key values later on we can do it all at

66
00:04:13,110 --> 00:04:13,730
once.

67
00:04:13,860 --> 00:04:18,360
We can open it enter and then we can add the first key.

68
00:04:18,440 --> 00:04:22,230
This is Shepherd's Bush which is in London.

69
00:04:22,500 --> 00:04:25,620
This their first key Shepherd's Bush.

70
00:04:26,100 --> 00:04:28,000
And we can give it a value.

71
00:04:28,410 --> 00:04:33,600
Would do this we use call on to assign a value we can say eight nine nine too.

72
00:04:33,690 --> 00:04:40,320
And in this case in our example we've seen the value represent the postcode of the key the key over

73
00:04:40,320 --> 00:04:42,110
here is Shepherd's Bush.

74
00:04:42,110 --> 00:04:44,700
And the value 8 9 9 2 is the postcode.

75
00:04:44,700 --> 00:04:45,120
Right.

76
00:04:45,120 --> 00:04:49,140
I know you get one scene and then we put a comma and then we add the next row.

77
00:04:49,140 --> 00:04:51,530
And over here we can add another area.

78
00:04:51,530 --> 00:04:54,330
We can see we can set those court

79
00:04:57,630 --> 00:05:03,060
and we can give it a value and the value here could be for 1 1 2.

80
00:05:03,060 --> 00:05:07,280
Then we add a comma we add a third area you can see Barbican

81
00:05:09,620 --> 00:05:13,070
and we give it a value.

82
00:05:13,420 --> 00:05:14,810
The value could be nice here.

83
00:05:14,860 --> 00:05:15,610
Eight seven.

84
00:05:16,830 --> 00:05:18,120
Right.

85
00:05:18,120 --> 00:05:20,250
So we've called our dictionary.

86
00:05:20,310 --> 00:05:20,580
Right.

87
00:05:20,610 --> 00:05:21,960
We shouldn't add more to find.

88
00:05:21,960 --> 00:05:29,550
Hit enter with close to Kelly braces in the same way we can print the contents of this dictionary by

89
00:05:29,550 --> 00:05:37,700
just coming down here and say print the name of the dictionary is postcodes post codes like this or

90
00:05:37,710 --> 00:05:43,200
press controls to save a click over here run and then run module and as you can see we've got examples

91
00:05:43,200 --> 00:05:47,420
example to Shepherd's Bush 8 9 9 2 which is the value.

92
00:05:47,430 --> 00:05:48,480
This is the key.

93
00:05:48,530 --> 00:05:50,040
We've got a second.

94
00:05:50,040 --> 00:05:55,890
The second key which is l scored 4 1 1 2 and then we've got Barbican 9 2 8 7.

95
00:05:55,890 --> 00:05:58,410
So there's another way of creating a dictionary.

96
00:05:58,620 --> 00:06:01,300
You can initialize it as you create it.

97
00:06:01,440 --> 00:06:03,630
That's also possible.

98
00:06:03,730 --> 00:06:06,510
So no now let's see a third example.

99
00:06:06,510 --> 00:06:09,410
Let's see how to iterate through a dictionary.

100
00:06:09,450 --> 00:06:15,670
So I'm going to bring a new head in here and I'm just put in this these headings for your revision.

101
00:06:15,690 --> 00:06:22,740
I call this a sample three as we saw in earlier lessons we iterate through a list using the for loop

102
00:06:23,370 --> 00:06:29,280
and for loop what we do is as soon as we use the four key word we follow it by an iterator variable

103
00:06:29,280 --> 00:06:30,360
that we create.

104
00:06:30,360 --> 00:06:39,430
So we can have for X for Y for C for boy for go for item for city for country.

105
00:06:39,510 --> 00:06:45,540
And these are these variables are variables that we just created without assigning types to them we

106
00:06:45,540 --> 00:06:52,320
can create iterator variables on the fly in the for statement and then use it to iterate through the

107
00:06:52,320 --> 00:07:00,600
list over here with dictionaries because with all keys and values we need to iterate two variables one

108
00:07:00,600 --> 00:07:06,790
would iterate through the Keys and the other one would iterate through the values that see what I mean.

109
00:07:06,810 --> 00:07:12,030
So I'm going to come down here and say for area.

110
00:07:12,030 --> 00:07:15,050
This is the first iterator coma code.

111
00:07:15,120 --> 00:07:26,640
This the second iterator in postcodes does the name of our dictionary postcodes dot item items the items

112
00:07:26,640 --> 00:07:32,370
like this will put us in the column and what we want to do we want to say print

113
00:07:36,900 --> 00:07:49,650
the post code of percentage as this percentage D and over here we can see percentage

114
00:07:52,440 --> 00:08:04,140
area code like this let's press control s and then let's run to see what we get from this is going to

115
00:08:04,140 --> 00:08:05,620
see this example three.

116
00:08:05,710 --> 00:08:11,960
It says the postcode of Shepherd's Bush is 8 9 9 2 The Post good of escort court is for 1 1 2.

117
00:08:12,100 --> 00:08:15,170
The Post could have Barbican become 9 0 8 7.

118
00:08:15,280 --> 00:08:16,450
So this is very simple.

119
00:08:16,450 --> 00:08:21,830
This how with iterated through the dictionary now lets your last example.

120
00:08:21,880 --> 00:08:28,890
What if we've we've already created our dictionary and we want to delete a row from the dictionary.

121
00:08:28,900 --> 00:08:30,380
How do we go about that.

122
00:08:30,400 --> 00:08:31,260
Let's do that now.

123
00:08:31,270 --> 00:08:33,500
I'll create a header here.

124
00:08:33,500 --> 00:08:37,390
I call this example number 4.

125
00:08:37,690 --> 00:08:38,170
No name.

126
00:08:38,170 --> 00:08:47,170
This deletion like this and we can just print.

127
00:08:49,210 --> 00:08:53,400
Example for her and to delete.

128
00:08:53,410 --> 00:09:03,020
We can simply say they'll use the deal and then we'll see post code and then we can just pass the key.

129
00:09:03,400 --> 00:09:04,200
The key here.

130
00:09:04,210 --> 00:09:11,040
Let's say we want to delete ls code from the database of post codes to see else quotes like this.

131
00:09:12,850 --> 00:09:16,940
And over here we have to use square brackets.

132
00:09:16,940 --> 00:09:19,750
No no more brackets with square brackets.

133
00:09:20,020 --> 00:09:20,290
OK.

134
00:09:20,320 --> 00:09:26,500
So down now that we've deleted our scores less print postcode less prints or a dictionary and see what

135
00:09:26,500 --> 00:09:27,110
we have.

136
00:09:27,250 --> 00:09:29,050
Print post.

137
00:09:29,050 --> 00:09:30,270
Goods.

138
00:09:30,500 --> 00:09:31,960
Control controls to save.

139
00:09:31,970 --> 00:09:33,250
Click here.

140
00:09:33,460 --> 00:09:41,920
Run module OK we've got a typo cause we say postcode but what dictionaries called postcodes.

141
00:09:41,960 --> 00:09:43,010
We forgot the s.

142
00:09:43,220 --> 00:09:49,550
So put the S over here press gone through esta safe click Koran and then click to run the module.

143
00:09:49,610 --> 00:09:55,530
Now example forces ships push 8 9 9 2 Barbican 9 0 8 7.

144
00:09:55,840 --> 00:09:58,490
You see there is no longer else court here.

145
00:09:58,520 --> 00:10:00,440
Compare this to this.

146
00:10:00,560 --> 00:10:03,390
This how we delete from the dictionary.

147
00:10:03,530 --> 00:10:06,350
So this all there is for dictionaries.

148
00:10:06,380 --> 00:10:11,720
And if you have any questions send me a message or leave it in the question its area and if you'll find

149
00:10:11,720 --> 00:10:17,780
in this course useful please take some time off to leave a review and I shall see you in the next lesson.
