1
00:00:05,600 --> 00:00:08,800
In this video, we'll examine the
problem with our shopping list.

2
00:00:08,800 --> 00:00:13,600
We'll look at why we've got a problem, but
more importantly perhaps, how to fix it.

3
00:00:13,600 --> 00:00:18,560
I'll start with an obvious improvement. It's not
perfect, but we can make our list less annoying by

4
00:00:18,560 --> 00:00:26,160
sorting it. So on line 42, I'm going to wrap shopping
list in parentheses and put sorted, so sorted,

5
00:00:26,160 --> 00:00:35,446
parentheses, shopping list as the argument. So
that's a very simple change. We run the program now.

6
00:00:36,080 --> 00:00:40,400
We enter the same options
as last time - that was 4

7
00:00:40,400 --> 00:00:47,680
Egg sandwich, 2 Chicken and
chips and 5 Beans on toast,

8
00:00:47,680 --> 00:00:50,434
zero to exit.

9
00:00:50,800 --> 00:00:54,000
Both the bread entries now appear
together, and our cook is no longer

10
00:00:54,000 --> 00:00:59,866
having to return to the bakers. Okay, so
that's a quick fix but we can do better.

11
00:01:00,320 --> 00:01:03,760
Our add_shopping_item function

12
00:01:03,760 --> 00:01:07,360
can check if the list already contains
an item, and update the quantity

13
00:01:07,360 --> 00:01:13,855
if it does. So what I'm going to do is go back
to here, line 42. I'm going to undo that change,

14
00:01:14,320 --> 00:01:18,870
and back up to our add_shopping_item
function on line 4.

15
00:01:20,000 --> 00:01:24,960
We decided to use a list of tuples, and searching
through the list is going to be a bit tricky.

16
00:01:24,960 --> 00:01:30,160
We saw how to use the index method of a list,
to find an item, but we don't know what item to

17
00:01:30,160 --> 00:01:36,720
search for. Items in the list are in two parts; an
ingredient and a quantity. We know the ingredient

18
00:01:36,720 --> 00:01:43,205
part that we want, but we don't know what
quantity it'll have, if it's already in the list.

19
00:01:43,840 --> 00:01:48,640
That means we can't search for a specific
tuple. We have to iterate over the list,

20
00:01:48,640 --> 00:01:54,080
and compare the first item in each tuple. Choosing
a list of tuples instead of a dictionary, wasn't

21
00:01:54,080 --> 00:01:59,120
the best decision here, but we produced a
working program, which doesn't do a bad job.

22
00:01:59,120 --> 00:02:02,320
A couple of videos ago, I suggested
that you should just pick one, if you

23
00:02:02,320 --> 00:02:07,840
can't decide which option is best. Here I
picked the wrong option - a list of tuples

24
00:02:07,840 --> 00:02:12,160
isn't as suitable as a dictionary, in this
case. But it's not the end of the world.

25
00:02:12,160 --> 00:02:18,790
Our program isn't as easy to modify as it could
be, but we did produce something that works.

26
00:02:19,440 --> 00:02:24,320
In the process, we've learned something.
A dictionary is more appropriate here.

27
00:02:24,320 --> 00:02:27,760
Always strive to produce the best
code - that goes without saying -

28
00:02:27,760 --> 00:02:33,360
but don't let indecision hold you back. Alright,
so time to fix the problem. So looking at line

29
00:02:33,360 --> 00:02:40,617
14, we want a dictionary instead of a list. So
let's change that so it's now a dictionary.

30
00:02:40,617 --> 00:02:47,760
And looking down here on line 40, we're
getting a warning; Expected type list,

31
00:02:47,760 --> 00:02:53,440
got dict instead. Our function is expecting a list,
but we're now passing a dictionary to it, or dict.

32
00:02:53,440 --> 00:02:58,664
We should change the annotations
and the Docstring in our function.

33
00:02:58,720 --> 00:03:07,760
We're gonna change this now, to a dict. I'll change
it over here now, our Docstring, to indicate

34
00:03:07,760 --> 00:03:13,280
that is now a dictionary. We've
now got a warning on line 6;

35
00:03:13,280 --> 00:03:18,480
Unresolved attribute reference append for
class dict. Problem is there, is a dictionary

36
00:03:18,480 --> 00:03:22,160
don't have an append method, but that's okay -
we're going to change the function code.

37
00:03:22,160 --> 00:03:27,520
This is also a great opportunity to see another
dictionary method. I'll do things the long way

38
00:03:27,520 --> 00:03:33,760
first, so that it's clear what the new method will
be doing. So let's delete that line, and replace it

39
00:03:33,760 --> 00:03:42,240
with if item in data colon, data square
brackets, and item in those square brackets

40
00:03:42,240 --> 00:03:50,720
plus equals amount. And we'll do an else otherwise
on the next line; else colon. It'll be data item in

41
00:03:50,720 --> 00:03:57,360
square brackets equals amount. So that's fairly
straightforward. If the item is in the dictionary,

42
00:03:57,360 --> 00:04:03,040
we're using augmented assignment to increase its
value by amount. Otherwise, using the else, we store

43
00:04:03,040 --> 00:04:08,800
the amount using item as the key. This adds the
entry to the dictionary. Because we're now using a

44
00:04:08,800 --> 00:04:14,000
dictionary instead of a list, we have to get the
keys and values. We have done that a few times

45
00:04:14,000 --> 00:04:19,230
now - we use the dictionary's items method. So
let's do that. So we're going to add down here;

46
00:04:19,230 --> 00:04:27,840
for things in shopping list.items, parentheses.
Let's run the program now and see what we've got.

47
00:04:28,800 --> 00:04:37,760
Same options as before; Egg sandwich, Chicken
and chips, Beans on toast, zero to exit.

48
00:04:37,760 --> 00:04:44,868
This time now, there's only one entry for bread
with a quantity of 120, so that's much better.

49
00:04:45,600 --> 00:04:49,440
More importantly, we didn't have to
make a lot of changes to the program.

50
00:04:49,440 --> 00:04:53,840
In fact, the only changes we made to the
main program were because of my bad decision.

51
00:04:53,840 --> 00:04:59,280
But how bad a decision was it really? We had to
make a few changes to the code to use a dictionary,

52
00:04:59,280 --> 00:05:03,760
instead of a list of tuples. But we haven't had
to completely rewrite everything. In fact, the

53
00:05:03,760 --> 00:05:08,400
changes we needed to make were relatively
minor. So hopefully, that's convinced you to

54
00:05:08,400 --> 00:05:14,400
just go ahead and start coding, whenever you
can't choose between more than one option.

55
00:05:14,400 --> 00:05:19,680
It didn't take long to realize that my decision
wasn't the best one, and it was easy to fix. 

56
00:05:19,680 --> 00:05:24,400
When you've gained more experience, and are working
on much larger programs, then changes will be

57
00:05:24,400 --> 00:05:29,280
more costly. But don't let indecision hold
you back, especially while you're learning.

58
00:05:29,280 --> 00:05:32,320
Have a go and make mistakes. You'll
learn a lot from your mistakes.

59
00:05:32,320 --> 00:05:36,800
You learn nothing from doing nothing, as
I've said previously. Alright, so moving on. 

60
00:05:36,800 --> 00:05:42,480
In the next video, we'll look at the alternative
code I mentioned. See you in that next video.

