1
00:00:05,440 --> 00:00:10,800
In the last video, I mentioned that there's a more
concise way to write our add_shopping_item

2
00:00:10,800 --> 00:00:16,400
function. So have a look at the
code on lines six through nine. We check to see

3
00:00:16,400 --> 00:00:22,800
if the item's in the dictionary, on line 6.
If it is, we add amount to the current value.

4
00:00:22,800 --> 00:00:27,120
We've used augmented assignment to do that -
augmented assignment works fine with values

5
00:00:27,120 --> 00:00:32,000
in a dictionary. Otherwise, we store amount
in the dictionary, creating a new entry

6
00:00:32,000 --> 00:00:39,120
with the key item. That sort of code is so common,
that Python's dictionaries provide a way to help.

7
00:00:39,120 --> 00:00:45,929
What I'm going to do is comment out these four
lines, and use the setdefault method instead.

8
00:00:46,800 --> 00:00:53,120
So I'm going to type data, left square bracket,
item in that left square bracket, equals then data

9
00:00:53,120 --> 00:01:05,000
dot setdefault. Then in parentheses, item comma
0, and outside of the parentheses put plus amount.

10
00:01:05,040 --> 00:01:09,600
The setdefault method returns the value
from the dictionary, if the key exists.

11
00:01:09,600 --> 00:01:14,800
If the key doesn't exist, it creates a new entry
for the key, assigns the default value to it,

12
00:01:14,800 --> 00:01:20,800
which is zero in this case, and then returns that
default value. We add amount to the value returned,

13
00:01:20,800 --> 00:01:25,840
then store the value back to the dictionary. In a
moment, we'll look at what it does in more detail.

14
00:01:25,840 --> 00:01:31,977
First though, let's run the program to
make sure that it still all works.

15
00:01:31,977 --> 00:01:41,440
4 for Egg sandwich, 2 for Chicken and
chips, 5 for Beans on toast, zero to finish.

16
00:01:41,440 --> 00:01:49,074
The output looks correct there; 120 for bread, 10
for butter, and so on. So looking back at the code,

17
00:01:49,074 --> 00:01:56,000
line 10 is doing the same job as lines 6 to
9, but is much more concise. It's also less

18
00:01:56,000 --> 00:02:01,840
obvious what it's doing, but don't let that put you
off using it. Using setdefault is a useful tool

19
00:02:01,840 --> 00:02:06,320
in your toolkit. Some more examples will
help with understanding what it does.

20
00:02:06,320 --> 00:02:13,390
Let's create a new Python file now. We're going
to call this one dict_defaults.

21
00:02:19,200 --> 00:02:23,200
Alright, so we don't want to type in another
dictionary here so I'm going to import pantry;

22
00:02:23,200 --> 00:02:29,806
from contents import pantry.

23
00:02:29,920 --> 00:02:35,120
Alright, so let's see how things work differently,
when we use setdefault to access a dictionary.

24
00:02:35,120 --> 00:02:38,160
We know we've got chicken in the
pantry, so I'm going to start with that.

25
00:02:38,160 --> 00:02:46,480
On line three I'm going to type chicken_quantity
is equal to pantry dot

26
00:02:46,480 --> 00:02:54,640
setdefault, and in parentheses, chicken in
double quotes, then comma zero. On the next line,

27
00:02:54,640 --> 00:03:02,960
print parentheses, we'll do an f string, and type
in the double quotes chicken colon then curly

28
00:03:02,960 --> 00:03:10,240
braces, and print out chicken_quantity.
Alright, so let's run the program,

29
00:03:10,240 --> 00:03:14,080
and you can see we get the
result 500 - that's correct.

30
00:03:14,080 --> 00:03:21,520
That's how much chicken we have. If we go
back and look at our contents.py again,

31
00:03:21,520 --> 00:03:30,160
there's chicken: 500 on line 2. So what happened
here? The setdefault method returns the value of a

32
00:03:30,160 --> 00:03:37,600
key, if the key exists in the dictionary. Chicken, as
we've said on line three, does exist so we got its

33
00:03:37,600 --> 00:03:43,760
value. So far, set default is behaving the same as
get. The difference is in what happens when the key

34
00:03:43,760 --> 00:03:49,440
doesn't exist in the dictionary. Let's type some
more code in - we'll do the same thing for beans.

35
00:03:49,440 --> 00:03:53,280
On line six, I'm going to type
beans_quantity

36
00:03:53,280 --> 00:03:58,560
equals pantry.setdefault, and in
parentheses, we'll type beans in double quotes,

37
00:03:58,560 --> 00:04:04,080
and a comma 0 again, then closing right parenthesis.
We'll do the same thing as we did on line 4 -

38
00:04:04,080 --> 00:04:09,680
we'll print it out. So print parentheses, do an
f string, then beans in the double quotes colon

39
00:04:09,680 --> 00:04:17,760
then curly braces beans_quantity,
and end of the line. Now if we run this,

40
00:04:17,760 --> 00:04:21,519
we get zero for the quantity of beans.

41
00:04:21,519 --> 00:04:28,000
The reason for that; beans is not in the
pantry dictionary, as you can see there.

42
00:04:28,000 --> 00:04:32,560
And because beans wasn't in the dictionary,
setdefault returned the default value that we

43
00:04:32,560 --> 00:04:38,240
specified on line six. That was zero, and that's the
value we get printed out. And once again, this looks

44
00:04:38,240 --> 00:04:45,000
similar to using the get method with a default
value. So we can actually check that, so let's try;

45
00:04:45,000 --> 00:04:55,680
ketchup_quantity is equal to
pantry.get this time, ketchup in double quotes

46
00:04:55,680 --> 00:05:02,000
comma zero. On the next line, same thing - we'll
print it out; print parentheses f-string,

47
00:05:02,000 --> 00:05:07,840
and in the double quotes, ketchup colon and
then curly braces ketchup_quantity,

48
00:05:07,840 --> 00:05:15,040
and end the line. And if we run this, you get
ketchup zero, and that's consistent with the fact

49
00:05:15,040 --> 00:05:21,200
that in our pantry dictionary, ketchup doesn't
exist. So the get method on line nine appears

50
00:05:21,200 --> 00:05:25,920
to be doing the same thing as setdefault on
line six. The difference, though, is that set

51
00:05:25,920 --> 00:05:31,360
default doesn't just return the default value -
it adds the key to the dictionary as well. We're

52
00:05:31,360 --> 00:05:35,760
going to print out the dictionary keys and
values so that we can check what it contains;

53
00:05:35,760 --> 00:05:45,520
line 12, print parentheses. Next line, print
parentheses double quotes. I'm going to type pantry,

54
00:05:45,520 --> 00:05:49,594
now single quotes, now contains.

55
00:05:51,040 --> 00:05:59,040
Then on line 15, for key comma value in
sorted parentheses that's pantry.items,

56
00:05:59,040 --> 00:06:05,920
parentheses, closing parenthesis and a
colon, and we'll print key comma value.

57
00:06:05,920 --> 00:06:11,905
And I've sorted the dictionary items, just to
make things easier to check. If we run this -

58
00:06:12,880 --> 00:06:19,840
on the second line of the output, we can see beans
there, now with the value zero. That key was added

59
00:06:20,640 --> 00:06:26,720
in line six of our code. But checking carefully,
we can see that there's no ketchup entry showing,

60
00:06:26,720 --> 00:06:32,000
which is correct, because the setdefault method
adds the key to the dictionary, if it doesn't exist.

61
00:06:32,000 --> 00:06:37,040
When it adds the key, it uses the default value
as the value to store in the dictionary. get, on

62
00:06:37,040 --> 00:06:42,080
the other hand, only returns the item with the
default value. It doesn't add it to the dictionary,

63
00:06:42,080 --> 00:06:47,040
if it doesn't exist. The setdefault method adds
the key to the dictionary, if it doesn't exist.

64
00:06:47,040 --> 00:06:51,920
When it adds the key, it uses the default
value as the value to store in the dictionary.

65
00:06:51,920 --> 00:06:59,200
It then returns the value that the key has. If the
key exists, such as chicken on line 3, then you get

66
00:06:59,200 --> 00:07:04,960
the value of the key - 500, in this case - the value
showing there for pantry, but also as the output,

67
00:07:04,960 --> 00:07:12,080
at the top of the screen there. That was the output
from the print on line four. If the key had to be

68
00:07:12,080 --> 00:07:16,640
added, you'll get the default value that you pass
to setdefault, and that's zero in the case of

69
00:07:16,640 --> 00:07:20,960
beans, and again, we can see that showing in the
output. The default value doesn't have to be a

70
00:07:20,960 --> 00:07:25,520
number - you can use anything that can be stored
in the dictionary. Let's have a look at that. What

71
00:07:25,520 --> 00:07:33,840
I'm going to do, is after ketchup, so here on
line 12, I'm going to type z_quantity

72
00:07:33,840 --> 00:07:42,480
is equal to pantry.setdefault double quotes zucchini,

73
00:07:42,480 --> 00:07:46,400
zucchini in those double quotes, comma. Then
we're going to put eight in double quotes

74
00:07:46,400 --> 00:07:51,600
this time, instead of a number, and we'll
print it out;  print parentheses an f string,

75
00:07:51,600 --> 00:07:57,360
in the double quotes zucchini colon, then
z_quantity in our curly

76
00:07:57,360 --> 00:08:01,920
braces, and close off the line. I've used
zucchini because it'll sort to the end of

77
00:08:01,920 --> 00:08:09,200
the output, and be easy to spot. Zucchini
is another name for courgette. Let's run this,

78
00:08:09,200 --> 00:08:13,040
and there's zucchini at the end of
the output with a string value 8.

79
00:08:13,040 --> 00:08:20,240
zucchini didn't exist in the dictionary, but was
added by the call to setdefault on line 12. Okay.

80
00:08:20,240 --> 00:08:25,680
So now I want to talk about ketchup again. On line
9 of our code, we attempt to retrieve the value

81
00:08:25,680 --> 00:08:31,046
for ketchup. We used the get method to do that,
and we got the default value of zero returned.

82
00:08:31,046 --> 00:08:36,000
You can see that in the third line of the output,
if you scroll up here. It's important to note that

83
00:08:36,000 --> 00:08:41,120
ketchup wasn't added to the dictionary. When
you use get, a non-existent key isn't added

84
00:08:41,120 --> 00:08:48,638
to the dictionary. When you use setdefault, a
non-existent key is added to the dictionary.

85
00:08:48,720 --> 00:08:55,200
If you google for python setdefault, you'll find
lots of posts talking about a defaultdict. 

86
00:08:55,200 --> 00:09:00,160
We're not going to look at defaultdict just yet. It's
one of the objects in the collections module, and

87
00:09:00,160 --> 00:09:03,920
it works very much like the dictionaries we've
been looking at. The difference is that it can

88
00:09:03,920 --> 00:09:10,570
automatically return a default value, and add
a key to the dictionary, if a key doesn't exist.

89
00:09:10,640 --> 00:09:15,680
Often, using a defaultdict can be a good
alternative to calling set default on a normal

90
00:09:15,680 --> 00:09:21,360
dictionary. The documentation for defaultdict
talks about subclassing and overriding methods.

91
00:09:21,360 --> 00:09:26,640
Those terms won't make sense until after the
Object-Oriented Programming section of this course.

92
00:09:26,640 --> 00:09:33,367
We'll look at the collections defaultdict class
later, once you've learned about classes in Python.

93
00:09:34,000 --> 00:09:37,120
Alright, so there's still a few dictionary
operations that we haven't looked at,

94
00:09:37,120 --> 00:09:41,840
but we've covered the most common things that
you'll want to do with dictionaries. We'll check

95
00:09:41,840 --> 00:09:46,400
out the documentation and look at the remaining
dictionary methods, after the next video. Before we

96
00:09:46,400 --> 00:09:50,560
do that, though, I'll demonstrate why our shopping
list is more useful than just printing out the

97
00:09:50,560 --> 00:09:56,720
values. If you remember, we modified the program to
create a shopping list dictionary, rather than just

98
00:09:56,720 --> 00:10:03,840
printing it. So in the next video, we'll see one
way that can be useful. See you in the next video.

