1
00:00:05,529 --> 00:00:09,710
Ok, now we'll start using dictionaries
for more interesting things.

2
00:00:09,710 --> 00:00:12,940
If you've got $8,000 to spare, you
could buy yourself a smart fridge.

3
00:00:12,940 --> 00:00:17,210
They keep track of the food in them, and
let you know what you can make for tea.

4
00:00:17,210 --> 00:00:21,029
We're going to use some Python dictionaries
to do something similar.

5
00:00:21,029 --> 00:00:26,340
We've created the data for you, so start by
downloading the contents.txt file, from the

6
00:00:26,340 --> 00:00:30,010
resources for this video.

7
00:00:30,010 --> 00:00:34,720
Once it downloads, open the file and copy
the entire contents to your clipboard.

8
00:00:34,720 --> 00:00:39,239
Back in IntelliJ, create a new
Python file called contents.py

9
00:00:39,239 --> 00:00:56,929
Paste in the code you've copied, and you'll have
the basic dictionaries that we're going to use.

10
00:00:56,929 --> 00:01:01,489
The first dictionary is called pantry, and
it contains the food and ingredients that

11
00:01:01,489 --> 00:01:02,760
we've got available.

12
00:01:02,760 --> 00:01:06,229
This is even better than an 8,000 dollar fridge,
because we're tracking things that a fridge

13
00:01:06,229 --> 00:01:07,420
wouldn't know about.

14
00:01:07,420 --> 00:01:09,270
You wouldn't normally keep
rice in a fridge, for example.

15
00:01:09,270 --> 00:01:14,490
It's also unlikely that you'd keep your spices
- cumin, paprika, etc - in the fridge.

16
00:01:14,490 --> 00:01:17,969
So we're going a step further
- we've got a smart pantry (smile).

17
00:01:17,969 --> 00:01:21,420
The dictionary contains the names
of the food items and ingredients.

18
00:01:21,420 --> 00:01:25,430
They're the keys, and the values
are how much of each one we've got.

19
00:01:25,430 --> 00:01:29,479
On line 2, you can see that
we've got 500 grammes of chicken.

20
00:01:29,479 --> 00:01:35,469
We've also got 2 lemons, on
line 3, and various spices.

21
00:01:35,469 --> 00:01:38,890
The oil, on line 8, is
measured in millilitres.

22
00:01:38,890 --> 00:01:43,070
If you're in America, you may have been thinking
that 450 fluid ounces is a lot of oil.

23
00:01:43,070 --> 00:01:47,070
450 ml is about 16 fluid ounces,
or roughly a pint.

24
00:01:47,070 --> 00:01:51,420
A bit less than 2 cups.

25
00:01:51,420 --> 00:01:56,960
Ok, the next dictionary contains
the ingredients for some recipes.

26
00:01:56,960 --> 00:02:06,829
The keys are the names of the meals - things
like Butter chicken, on line 24, and Chicken

27
00:02:06,829 --> 00:02:08,840
and chips, on line 41.

28
00:02:08,840 --> 00:02:12,980
The meals get easier to cook, as you
get further down the dictionary.

29
00:02:12,980 --> 00:02:18,330
We finish with Spam a la tin, on line 59.

30
00:02:18,330 --> 00:02:22,220
That's quite an easy meal to prepare - you
open the tin and dive in with a spoon.

31
00:02:22,220 --> 00:02:27,900
I mention that because this is the order that
we'll be presenting the meals, to start with.

32
00:02:27,900 --> 00:02:32,099
I'll then show you how to sort the keys in
a dictionary, so that we can present the meals

33
00:02:32,099 --> 00:02:34,110
in alphabetical order.

34
00:02:34,110 --> 00:02:35,720
But we'll start off with this order.

35
00:02:35,720 --> 00:02:42,310
Before we start using these dictionaries,
take a closer look at recipes.

36
00:02:42,310 --> 00:02:45,019
The keys are the name of each recipe, or meal.

37
00:02:45,019 --> 00:02:46,810
The values are lists.

38
00:02:46,810 --> 00:02:50,840
Although there are restrictions on what you
can use as a dictionary key - which I'll talk

39
00:02:50,840 --> 00:02:54,880
about soon - you can use any
Python object as a value.

40
00:02:54,880 --> 00:02:56,250
We're using lists here.

41
00:02:56,250 --> 00:03:01,390
You can see the value for Butter
Chicken on lines 25 to 39.

42
00:03:01,390 --> 00:03:04,939
You can store tuples in a dictionary,
or even other dictionaries.

43
00:03:04,939 --> 00:03:08,680
Dictionary values can be anything
that Python knows about.

44
00:03:08,680 --> 00:03:11,890
That's pretty cool, and lets us
do all sorts of useful things.

45
00:03:11,890 --> 00:03:13,810
But we're starting off quite simple here.

46
00:03:13,810 --> 00:03:19,670
Our values are lists - specifically, lists
of the ingredients that we need for each recipe.

47
00:03:19,670 --> 00:03:22,930
If we want, we could extend that to
also include the recipe instructions.

48
00:03:22,930 --> 00:03:26,930
But it's a bit early for that, so we'll stick
with just the list of ingredients.

49
00:03:26,930 --> 00:03:32,519
On line 25, the first ingredient for Butter
Chicken is chicken, unsurprisingly.

50
00:03:32,519 --> 00:03:41,250
chicken is a key in the pantry
dictionary, on line 2.

51
00:03:41,250 --> 00:03:47,540
Each of the items in the list, on lines 25
to 39, must exist in the pantry dictionary,

52
00:03:47,540 --> 00:03:49,260
if we're going to make Butter Chicken.

53
00:03:49,260 --> 00:03:53,250
Data that's organised like this is
very common - and incredibly useful.

54
00:03:53,250 --> 00:03:56,709
It's common to have something like a list,
that contains keys in a dictionary.

55
00:03:56,709 --> 00:04:00,660
You might also have one dictionary, whose
values are keys in another dictionary.

56
00:04:00,660 --> 00:04:02,680
That would be useful here, in fact.

57
00:04:02,680 --> 00:04:07,400
Our recipes are a bit useless, because they
don't specify how much chicken we need.

58
00:04:07,400 --> 00:04:10,189
We'll extend this data soon,
and address that problem.

59
00:04:10,189 --> 00:04:13,990
But we're starting off simple, until we've
seen how these dictionaries work together.

60
00:04:13,990 --> 00:04:16,290
Alright, that's our data.

61
00:04:16,290 --> 00:04:20,940
In the next video, we'll start to
make use of these two dictionaries.

