1
00:00:00,000 --> 00:00:03,990
Python has one more very powerful aggregate type.

2
00:00:04,000 --> 00:00:08,990
It's called Dictionary, and it's much like what we would call an associative array

3
00:00:09,000 --> 00:00:11,990
or a hash in another language.

4
00:00:12,000 --> 00:00:15,990
So let's go ahead and make a working copy of variables.py. We'll call this

5
00:00:16,000 --> 00:00:23,990
variables-dictionary.py.

6
00:00:24,000 --> 00:00:27,990
Go ahead and open up that working copy, and we will start by defining a

7
00:00:28,000 --> 00:00:35,990
dictionary, and I will call it d, and when you use the curly brace notation for

8
00:00:36,000 --> 00:00:48,990
defining it, and we will use names for keys and numbers for values.

9
00:00:49,000 --> 00:01:01,990
Just going to put five of them in here, and space that out just a little bit and

10
00:01:02,000 --> 00:01:06,990
then we will - say, we will print it first here, d, and we'll go ahead and

11
00:01:07,000 --> 00:01:10,990
we'll save and run, and you'll see there is our dictionary.

12
00:01:11,000 --> 00:01:14,990
Now, this can be useful for a number of things.

13
00:01:15,000 --> 00:01:21,990
For example, we can step through the keys and print values, for k in d,

14
00:01:22,000 --> 00:01:25,990
and print.

15
00:01:26,000 --> 00:01:31,990
print the key, and we will print the value, which would be d k like this, and if

16
00:01:32,000 --> 00:01:35,990
we save that and run it, you will see that we get each of the keys and each of

17
00:01:36,000 --> 00:01:40,990
the values from our print statement, so we've iterated through the dictionary,

18
00:01:41,000 --> 00:01:47,990
and it's given us the key in the k variable, and we've used that to subscript

19
00:01:48,000 --> 00:01:49,990
and to print out the value.

20
00:01:50,000 --> 00:01:54,990
Now this is nice, but notice that it's in no particular order,

21
00:01:55,000 --> 00:01:56,990
that it's not alphabetical.

22
00:01:57,000 --> 00:01:57,990
It's not numerical.

23
00:01:58,000 --> 00:02:02,990
It's in no particular order whatsoever, and this is common for hashed variables

24
00:02:03,000 --> 00:02:05,990
in virtually all environments that supports such.

25
00:02:06,000 --> 00:02:11,990
So in Python, if you want to get this in assorted order, use the Sorted built-in

26
00:02:12,000 --> 00:02:19,990
function and you subscript the dictionary object and use its keys method.

27
00:02:20,000 --> 00:02:24,990
So sorted(d.keys()) like that, save it and run it,

28
00:02:25,000 --> 00:02:27,990
and now they are in alphabetical order by the keys.

29
00:02:28,000 --> 00:02:31,990
Five, four, one, three, and two.

30
00:02:32,000 --> 00:02:34,990
those are alphabetical by the keys.

31
00:02:35,000 --> 00:02:39,990
Now this particular syntax for defining the dictionary is a little bit

32
00:02:40,000 --> 00:02:41,990
awkward, don't you think?

33
00:02:42,000 --> 00:02:45,990
Wouldn't it be nice if there is a better one? Well there is.

34
00:02:46,000 --> 00:02:50,990
Python has something called keyword arguments, and we will learn about those

35
00:02:51,000 --> 00:02:53,990
more in our chapter on functions.

36
00:02:54,000 --> 00:02:58,990
But the keyword arguments are used to a great advantage in defining dictionary

37
00:02:59,000 --> 00:03:05,990
objects because we can do it like this, say dict using the constructor for the

38
00:03:06,000 --> 00:03:09,990
dictionary class, and I am just going to go ahead and put that closing

39
00:03:10,000 --> 00:03:14,990
parenthesis on a separate line and go ahead and show you how this works.

40
00:03:15,000 --> 00:03:17,990
So I can say one = 1, two = 2.

41
00:03:18,000 --> 00:03:21,990
Notice I don't have to type any of those nasty quotes.

42
00:03:22,000 --> 00:03:25,990
Three = 3, four = 4.

43
00:03:26,000 --> 00:03:29,990
Now if I had string objects that I was using at the values, of course, I would

44
00:03:30,000 --> 00:03:37,990
need quote, so I could say five = and put the number five in quotes, like that.

45
00:03:38,000 --> 00:03:43,990
And if I save this and run it, you will notice that this works exactly the same

46
00:03:44,000 --> 00:03:46,990
way, except we have this spelled out, word five there.

47
00:03:47,000 --> 00:03:52,990
So that's a much easier, more convenient, and a more common way to define a dictionary.

48
00:03:53,000 --> 00:03:57,990
Dictionaries are mutable objects, so I can add a new value to a dictionary, like

49
00:03:58,000 --> 00:04:09,990
this, d 'seven' = 7, and save that, and run it, and now we have the seven value

50
00:04:10,000 --> 00:04:13,990
in the middle there in our sorted list.

51
00:04:14,000 --> 00:04:17,990
So that's dictionaries in a nutshell. They are very powerful and very useful,

52
00:04:18,000 --> 00:04:28,000
and you will see, when we get to our example code, they get used a lot in Python.

