1
00:00:01,170 --> 00:00:06,560
In this lecture, I'm going to show you how to use dictionaries in Python, the dictionary is a data

2
00:00:06,570 --> 00:00:13,080
structure that allows you to store key and value pairs as long as the case is neat and there's no limit

3
00:00:13,080 --> 00:00:16,170
to how many of those keys and value pairs you can store.

4
00:00:16,560 --> 00:00:19,200
Going to give you a simple example to begin with.

5
00:00:19,590 --> 00:00:25,740
So let's say that you're building an application that keeps track of the specifications of your E.S.P

6
00:00:25,740 --> 00:00:26,370
32.

7
00:00:26,850 --> 00:00:30,560
You could use a dictionary like this one here.

8
00:00:30,570 --> 00:00:40,250
There are about Paiste girl that contains information about the E.S.P 32 information about the manufacturer,

9
00:00:40,590 --> 00:00:44,310
the type of device it is that should be used using and so on.

10
00:00:44,370 --> 00:00:49,710
So as you can see, I've got multiple keys like manufacturer with.

11
00:00:50,810 --> 00:00:59,150
The value for that key, so there's another key here type and its value is microcontroller is a key

12
00:00:59,150 --> 00:01:05,060
city, you and the value your piece of string in this case, of course, you can have values that are

13
00:01:05,060 --> 00:01:07,710
numerical or a booleans, et cetera.

14
00:01:08,750 --> 00:01:19,790
If you check out the contents of this dictionary, just type in its name, you'll see that it starts

15
00:01:19,790 --> 00:01:24,800
with a curly bracket, finishes with a curly bracket, and then you've got the key and the values in

16
00:01:24,800 --> 00:01:26,030
between four.

17
00:01:26,030 --> 00:01:28,010
Click on the dictionary.

18
00:01:28,160 --> 00:01:31,170
It appears as well in the object, Inspector.

19
00:01:31,200 --> 00:01:33,950
So you can see it in a table format.

20
00:01:35,390 --> 00:01:40,520
Now you can do a few operations on a dictionary once you have it.

21
00:01:41,090 --> 00:01:48,510
For example, a common one is to use the list command and pass in the name of the dictionary.

22
00:01:48,740 --> 00:01:55,820
And what this is going to do is to bring back or give you create a list that contains the individual

23
00:01:55,820 --> 00:01:58,750
keys that are part of this dictionary.

24
00:01:59,200 --> 00:02:01,530
This you can see the square brackets.

25
00:02:01,550 --> 00:02:02,510
This is a list.

26
00:02:02,510 --> 00:02:09,980
You can use it in a iterator like a folded, for example, so that you can iterate through all of the

27
00:02:09,980 --> 00:02:18,110
keys of the dictionary and perhaps print out the values for each key or do some other kind of text processing.

28
00:02:19,440 --> 00:02:25,740
Of course, you can extract the individual values for each given key.

29
00:02:26,160 --> 00:02:37,860
So, for example, if you wanted to print out the RAND value or the value stored in the RAND key of

30
00:02:37,860 --> 00:02:44,400
the dictionary, then you can use the square brackets and then in between the square brackets, type

31
00:02:44,400 --> 00:02:48,860
in the name of the key that you want to get the value of it into.

32
00:02:48,870 --> 00:02:55,320
And you will give you that specific value, which is this one right here.

33
00:02:56,760 --> 00:03:04,680
Dictionaries are mutable, which means that you can make changes to the values of existing keys or you

34
00:03:04,680 --> 00:03:07,210
can add new key and value pairs.

35
00:03:07,620 --> 00:03:13,790
So, for example, I can make a small change to the brand that you can see here.

36
00:03:14,520 --> 00:03:21,060
I'm making reference to the particular key that I want to change the value of the material change to

37
00:03:21,060 --> 00:03:23,490
the value stored there originally.

38
00:03:24,100 --> 00:03:28,640
That's five hundred and twelve kilobytes of static ram.

39
00:03:28,650 --> 00:03:33,030
Let's make that five twenty one for the sake of the argument hit enter.

40
00:03:33,060 --> 00:03:39,840
So now we've got a new value for that Calixto against that key.

41
00:03:40,440 --> 00:03:44,310
So in other words, dictionaries are mutable.

42
00:03:44,890 --> 00:03:46,890
You can also use.

43
00:03:48,100 --> 00:03:57,760
A Syntex like this, in order to check that a particular case exists inside a dictionary so you can

44
00:03:57,760 --> 00:04:04,400
say the name of the key and check using the key word if it exists inside the dictionary.

45
00:04:04,420 --> 00:04:06,370
In this case, it does exist.

46
00:04:06,910 --> 00:04:11,920
If I search for something that does not exist, then.

47
00:04:14,850 --> 00:04:16,680
You'll get folks, it's expected.

48
00:04:17,680 --> 00:04:23,260
Now, the easiest way, as I showed you the beginning, the easiest way to create a new dictionary is

49
00:04:23,260 --> 00:04:27,390
to use the curly brackets and then the key values inside the curly brackets.

50
00:04:27,400 --> 00:04:33,790
But there's also a constructor that you can use, which can be an example here.

51
00:04:35,040 --> 00:04:43,610
Expect this to this constructor uses the dictionary with Dict Keywood, and then you've got the parentheses

52
00:04:44,180 --> 00:04:52,940
for the keyword and for the square brackets open and and then inside parentheses, you list the key

53
00:04:52,940 --> 00:04:54,320
value pairs like that.

54
00:04:55,940 --> 00:04:58,100
And that has the same result.

55
00:04:58,490 --> 00:05:06,400
Eventually, he has a new dictionary that looks like this, just like the one that we created up there

56
00:05:06,740 --> 00:05:09,920
using two different ways to achieve the same result.

57
00:05:11,060 --> 00:05:13,780
And that's how you use dictionaries completely.
