1
00:00:05,280 --> 00:00:09,520
Our loop, from the last video, isn't 
very efficient. That's because it 

2
00:00:09,520 --> 00:00:15,360
creates a new set, each time round the loop.
If we check the documentation again, the union, 

3
00:00:15,360 --> 00:00:20,791
intersection, difference, and symmetric_difference 
operations all return a new set. 

4
00:00:20,791 --> 00:00:26,080
This part of the documentation lists the methods 
(and operators) that can be used with both sets, 

5
00:00:26,080 --> 00:00:32,400
and frozen sets. Frozen sets are immutable sets.
Because they're immutable, there are no methods 

6
00:00:32,400 --> 00:00:35,809
here that update a set.
Scrolling down a short way, 

7
00:00:35,809 --> 00:00:40,880
there's a list of operations that apply only 
to sets – you can't use these with frozen sets. 

8
00:00:40,880 --> 00:00:44,160
The first one is update.
For consistency with the three 

9
00:00:44,160 --> 00:00:50,491
methods that follow it, it should probably have 
been called union_update. Because that's what it is.

10
00:00:50,491 --> 00:00:56,080
It performs a set union, but modifies the 
original set, rather than creating a new one. 

11
00:00:56,080 --> 00:01:00,320
If we change our code to use the update 
method, or the update operator, we can avoid 

12
00:01:00,320 --> 00:01:06,481
creating a new set each time round the loop.
I'll comment out line 19, and use update: 

13
00:01:15,200 --> 00:01:16,808
Run the program, 

14
00:01:18,800 --> 00:01:26,720
to see the same result as we had last time.
That's more efficient. We update an existing set, 

15
00:01:26,720 --> 00:01:30,960
instead of creating a new one each time.
Sometimes, you will want to create a 

16
00:01:30,960 --> 00:01:36,560
new set. In that case, you'd use the 
union method – or the union operator. 

17
00:01:36,560 --> 00:01:40,160
Here, we were effectively 
updating the meds_to_watch set, 

18
00:01:40,160 --> 00:01:44,720
so it makes more sense to use the update method.
As with all these set operations, 

19
00:01:44,720 --> 00:01:50,000
there's also an operator to do the same thing. 
I'll comment out line 20 and use the operator: 

20
00:01:56,480 --> 00:02:01,090
When we covered augmented assignment, back in 
the Program flow control in Python section, 

21
00:02:01,090 --> 00:02:04,410
I mentioned that there were a few 
more augmented assignment operators. 

22
00:02:04,410 --> 00:02:07,879
This is one of four more, 
that we can use with sets. 

23
00:02:07,879 --> 00:02:14,000
Ok, I'll run that to check, before we move on.
and we get the same result. 

24
00:02:20,240 --> 00:02:22,960
In the next video, I'll discuss 
the advantage that the set 

25
00:02:22,960 --> 00:02:28,160
operation methods have, over the operators.
Before you start watching the next video, 

26
00:02:28,160 --> 00:02:32,925
create some sets of your own, and practise 
with the union and update operations. 

27
00:02:35,280 --> 00:02:40,000
For example, you might create these sets:
and create a set of things that bite, 

28
00:02:40,000 --> 00:02:45,120
or things that sting. Spiders 
bite, scorpions and vespas sting. 

29
00:02:45,120 --> 00:02:48,480
Spiders and scorpions are arachnids, 
so you could get a set of arachnids 

30
00:02:48,480 --> 00:02:57,280
by forming the union of those two sets.
Experiment, and I'll see you in the next video.

