1
00:00:05,280 --> 00:00:10,429
We've seen the theory of set union, and 
we've seen how to create a union, in Python. 

2
00:00:10,480 --> 00:00:14,677
In this video, we'll have a look at 
a practical application for all this. 

3
00:00:15,840 --> 00:00:23,280
Open up prescription_data.py, and have a look at 
the adverse_interactions list, on lines 17 to 25. 

4
00:00:23,280 --> 00:00:29,840
We've got a list. Each item in the list is a set, 
containing drugs that shouldn't be taken together. 

5
00:00:32,080 --> 00:00:36,800
Now, imagine you're a dispensing chemist, and 
you've got a queue of people at the counter. 

6
00:00:36,800 --> 00:00:41,627
You're about to hand over some Carbimazole.
How quickly can you check that list, to see if 

7
00:00:41,627 --> 00:00:48,000
Carbimazole has an adverse reaction with anything?
Pause the video, and see how long it takes. 

8
00:00:50,160 --> 00:00:53,730
That wasn't very easy, and there 
are only eight drugs in our list. 

9
00:00:53,840 --> 00:00:57,637
Imagine if it contained all drugs 
that have reactions with other drugs. 

10
00:00:57,637 --> 00:01:02,374
To help our dispensing chemists, we'll 
create an alphabetical list of these drugs. 

11
00:01:02,374 --> 00:01:06,373
An alphabetical list is much 
quicker for a human to work with. 

12
00:01:06,880 --> 00:01:12,884
Ok, switch back to our set_union.py file, 
and comment out all the existing code. 

13
00:01:13,360 --> 00:01:16,148
We'll import the adverse_interactions list, 

14
00:01:16,148 --> 00:01:19,108
then see how we can produce 
an alphabetical list from it: 

15
00:01:26,160 --> 00:01:31,119
The list contains seven sets, and we want 
to extract all the drugs from those sets. 

16
00:01:31,119 --> 00:01:34,580
To do that, we want to create 
the union of all the sets. 

17
00:01:34,580 --> 00:01:37,856
We'll start with an empty set, to hold the union:

18
00:01:43,929 --> 00:01:49,267
Next, we'll iterate over all the sets in the list, and use the union method to combine them:

19
00:02:06,872 --> 00:02:16,315
That's a very naive implementation, and we'll improve it shortly. First, let's see if it works. I'll sort the output, then print the set: 

20
00:02:26,080 --> 00:02:30,720
Run the program,
and check the output. 

21
00:02:30,720 --> 00:02:35,840
We've got a list containing all 
8 drugs, in alphabetical order. 

22
00:02:41,360 --> 00:02:46,960
On line 18, we use the union method to create a 
new set. The new set is the union of the next set 

23
00:02:46,960 --> 00:02:50,960
in the list, with whatever we've built up so far.
We then bind the variable 

24
00:02:50,960 --> 00:02:57,062
meds_to_watch to this new set.
As I said, this is quite a naïve way to do it. 

25
00:02:57,062 --> 00:03:00,329
Before we look at an improvement, 
I'll comment out line 18, 

26
00:03:00,329 --> 00:03:03,952
and do the same thing with the union operator:

27
00:03:12,145 --> 00:03:16,543
Once again, when I run it, we get the same result.

28
00:03:21,760 --> 00:03:26,800
Ok, that's a good example of when you might 
want to create a union of several sets. 

29
00:03:27,760 --> 00:03:31,993
Another example might be the trial 
we looked at, a few videos ago. 

30
00:03:31,993 --> 00:03:36,416
When evaluating the performance of a new 
drug, you might have 3 sets of people. 

31
00:03:36,416 --> 00:03:41,243
In fact, that's the case with lots of 
research, not just clinical trials. 

32
00:03:41,243 --> 00:03:45,610
One group would receive the new drug – or 
whatever it was that was being researched. 

33
00:03:45,610 --> 00:03:49,515
A second group would be given a 
placebo – a pill that does nothing. 

34
00:03:49,515 --> 00:03:55,392
And in some cases, you'd have a third group who 
didn't even know they were part of the research. 

35
00:03:56,880 --> 00:04:02,080
Often, you get their data by using 
historical data, from the general population. 

36
00:04:02,080 --> 00:04:05,296
In that case, the names would be anonymised. 

37
00:04:05,296 --> 00:04:08,800
That gives three sets, and you could use 
a set union to produce a set containing 

38
00:04:08,800 --> 00:04:12,000
all the data in the trial.
In the next video, 

39
00:04:12,000 --> 00:04:19,519
we'll improve this code slightly. We'll look 
at another set operation, union update.

