1
00:00:00,000 --> 00:00:04,990
Dictionaries are Python's version of associative arrays or hashed arrays.

2
00:00:05,000 --> 00:00:07,990
Dictionaries are created in a couple of different ways.

3
00:00:08,000 --> 00:00:23,990
We can create a dictionary with the curly braces and we can say key value like this.

4
00:00:24,000 --> 00:00:26,990
So now, we have a dictionary with those values in it.

5
00:00:27,000 --> 00:00:30,990
Actually, an easier way to create dictionaries is with its constructor because

6
00:00:31,000 --> 00:00:35,990
this allows you to use the named parameters feature of Python and it's just a

7
00:00:36,000 --> 00:00:36,990
whole lot easier to type.

8
00:00:37,000 --> 00:00:45,990
So you will see it done this way more often.

9
00:00:46,000 --> 00:00:53,990
We get exactly the same result and see that that type is a class dictionary.

10
00:00:54,000 --> 00:00:58,990
Finally, if you want to, you can even initialize one dictionary from another.

11
00:00:59,000 --> 00:01:10,990
If I have another dictionary?

12
00:01:11,000 --> 00:01:13,990
And so there I have that dictionary. I can say d =

13
00:01:14,000 --> 00:01:20,990
dictionary and I can take these definitions from up here, copy those and paste

14
00:01:21,000 --> 00:01:25,990
those in, and then I can actually specify that other dictionary with the two

15
00:01:26,000 --> 00:01:30,990
asterisk notation and I get a dictionary that has all of that in it.

16
00:01:31,000 --> 00:01:38,990
You can test if a value is in a dictionary by saying 'four' in x and I see that

17
00:01:39,000 --> 00:01:45,990
that's True and if I say 'three' in x that will not be true, because x here has

18
00:01:46,000 --> 00:01:47,990
just four, five and six in it.

19
00:01:48,000 --> 00:01:50,990
Likewise, you can iterate over a dictionary.

20
00:01:51,000 --> 00:01:53,990
You can say four k in d:

21
00:01:54,000 --> 00:01:58,990
print (k) and that will print out all of the keys.

22
00:01:59,000 --> 00:02:11,990
If you want to, you can say for k key and value in d.items print k and value.

23
00:02:12,000 --> 00:02:15,990
And that will give you both the keys and the values all in one fell swoop.

24
00:02:16,000 --> 00:02:20,990
If you want to find a particular item in a dictionary, one way to do it is to

25
00:02:21,000 --> 00:02:24,990
simply subscript it.

26
00:02:25,000 --> 00:02:28,990
The problem with that is that if your dictionary does not have that key, you

27
00:02:29,000 --> 00:02:29,990
will get an exception.

28
00:02:30,000 --> 00:02:34,990
If I try x sub 'three', I will get an exception.

29
00:02:35,000 --> 00:02:38,990
The way around that is to use the get method.

30
00:02:39,000 --> 00:02:45,990
If I say x.get('three'), it will simply silently give me the none value.

31
00:02:46,000 --> 00:02:51,990
If I say d.get('three') then I get the value.

32
00:02:52,000 --> 00:02:56,990
Another benefit of this is using the get method I can give it a default

33
00:02:57,000 --> 00:03:06,990
value. I can say give me that value of three or give me some default value.

34
00:03:07,000 --> 00:03:07,990
And I get the default value.

35
00:03:08,000 --> 00:03:10,990
Here I typed in the text 'not found'.

36
00:03:11,000 --> 00:03:15,990
If I simply want to delete an item from a dictionary, I can just look at the x here.

37
00:03:16,000 --> 00:03:17,990
I've got those items there.

38
00:03:18,000 --> 00:03:29,990
I can say del x' sub four' and now that's gone or I can say x.pop('five') and it will

39
00:03:30,000 --> 00:03:33,990
give me the value and delete it at the same time.

40
00:03:34,000 --> 00:03:34,990
So those are the major functions of dictionaries.

41
00:03:35,000 --> 00:03:37,990
Dictionaries are tremendously useful.

42
00:03:38,000 --> 00:03:43,990
Whenever you need to organize your data in such a way that it's easy to find,

43
00:03:44,000 --> 00:03:47,990
you can certainly store any kind of data in a list or in a tuple.

44
00:03:48,000 --> 00:03:50,990
But the indexes are always going to be numeric.

45
00:03:51,000 --> 00:03:53,990
The advantage of the dictionaries is that the indexes are data.

46
00:03:54,000 --> 00:03:57,990
So you can index it on any kind of data.

47
00:03:58,000 --> 00:04:02,990
Usually, you will use text, usually you will use words, but this allows you to

48
00:04:03,000 --> 00:04:06,990
basically create your own named spaces and organize them.

49
00:04:07,000 --> 00:04:07,990
They can be hierarchical.

50
00:04:08,000 --> 00:04:11,990
You can store dictionaries within dictionaries, you can store lists and tuples

51
00:04:12,000 --> 00:04:16,990
within dictionaries, and you will find as you will look through the examples

52
00:04:17,000 --> 00:04:20,990
of the code that I have written, I will often times carry one dictionary in a

53
00:04:21,000 --> 00:04:26,990
global namespace and put all of my data inside of that and then it becomes manageable.

54
00:04:27,000 --> 00:04:31,990
It's like a halfway point between flat data and an object with methods.

55
00:04:32,000 --> 00:04:34,990
Obviously, there is going to be times you are going to want to create an

56
00:04:35,000 --> 00:04:38,990
object with methods to handle your data in very specific ways, but for just

57
00:04:39,000 --> 00:04:42,990
organizing a whole lot of global variables or flags, you will find that

58
00:04:43,000 --> 00:04:48,990
dictionaries are very useful and for organizing object data within an object,

59
00:04:49,000 --> 00:04:51,990
you will find that dictionaries are very, very useful and very, very commonly

60
00:04:52,000 --> 00:05:02,000
used in these kinds of situations.

