1
00:00:05,200 --> 00:00:09,360
For this example, we're going to select 
candidates to interview for a job. 

2
00:00:09,360 --> 00:00:13,120
We've identified a list of core skills 
that successful candidates must have, 

3
00:00:13,120 --> 00:00:15,840
in order to perform the role we're hiring for. 

4
00:00:15,840 --> 00:00:18,960
I've created a new Python 
file called candidates.py, 

5
00:00:18,960 --> 00:00:25,668
for this example. Pause the video while you create 
your file, then type the required_skills list. 

6
00:00:40,000 --> 00:00:44,000
I've kept the list short, to make 
it easier to verify our results. 

7
00:00:44,000 --> 00:00:47,120
The HR department have processed 
all the job applications, 

8
00:00:47,120 --> 00:00:51,397
and their system has produced a dictionary 
containing the details for the candidates: 

9
00:03:02,720 --> 00:03:07,268
We need to check the skills for each candidate, 
to decide whether or not to interview them. 

10
00:03:07,268 --> 00:03:12,480
We'll create an empty set, to hold the names 
of the interviewees, then check their skills. 

11
00:03:12,480 --> 00:03:16,526
I'll type the code first, then 
explain how we perform that check: 

12
00:03:50,480 --> 00:03:54,160
Ok, how does this work ?
We iterate over the candidates 

13
00:03:54,160 --> 00:03:58,240
in the dictionary, to get each 
candidate name, and their skills. 

14
00:03:58,240 --> 00:04:03,440
As you can see on lines 4 to 9, the skills are 
stored as sets. This approach would still work 

15
00:04:03,440 --> 00:04:08,720
if they were stored in lists or tuples - we'd 
just use the set function to convert them first. 

16
00:04:08,720 --> 00:04:14,480
Or, we could convert our required_skills to a 
set, and perform the check the other way round. 

17
00:04:14,480 --> 00:04:19,040
Next, we want to check that the candidates' 
skills contain all the skills we require. 

18
00:04:19,040 --> 00:04:23,920
If a set contains all the items from another 
set, then it's a superset of the other set. 

19
00:04:23,920 --> 00:04:27,760
The check is very simple - we 
just use the issuperset method. 

20
00:04:27,760 --> 00:04:31,920
If it returns True, we add the 
candidate to our set of interviewees. 

21
00:04:31,920 --> 00:04:36,480
That's checked on line 14.
If you try to do this without using sets, you'll 

22
00:04:36,480 --> 00:04:42,640
end up writing nested loops, and a lot more code. 
Sets are very powerful for this sort of problem. 

23
00:04:42,640 --> 00:04:46,480
Ok, does it work ?
Looking at our candidates' skills, 

24
00:04:46,480 --> 00:04:51,452
Anna has everything we need. Bob also 
has the 3 skills we're looking for. 

25
00:04:51,452 --> 00:04:55,840
Daniel doesn't - he hasn't listed 
Python or Linux amongst his skills. 

26
00:04:55,840 --> 00:05:00,960
Ekani also has what we're looking for.
Fenna doesn't have Python – which is a shame, 

27
00:05:00,960 --> 00:05:04,960
because she has some impressive skills 
there. Python has borrowed aspects of 

28
00:05:04,960 --> 00:05:09,200
all the languages she's listed, so a 
human might well decide to interview her. 

29
00:05:09,200 --> 00:05:12,748
But I digress, let's run the 
program and see what we get. 

30
00:05:14,800 --> 00:05:20,080
We get the four candidates that we 
expect: Anna, Bob, Carol, and Ekani. 

31
00:05:20,080 --> 00:05:25,120
If there were a lot of candidates, this could be 
a way to select a small enough set to interview. 

32
00:05:25,120 --> 00:05:28,240
You could also use this approach 
in an on-line dating app. 

33
00:05:28,240 --> 00:05:33,440
Tinder uses the Python language for some of its 
back-end server code. Of course, I can't say 

34
00:05:33,440 --> 00:05:38,625
that they use Python sets to group potential 
matches, but you can see how that could work. 

35
00:05:38,625 --> 00:05:43,569
I'll finish this video with an example of the difference 
between a subset, and a proper subset. 

36
00:05:43,569 --> 00:05:49,037
This data gives us an opportunity to see 
the difference in practice. What a coincidence! 

37
00:05:49,037 --> 00:05:54,400
We're very short of time, and can't spare people 
to interview many candidates. We need to reduce 

38
00:05:54,400 --> 00:05:58,560
the number of candidates we've selected.
At the moment, we're matching anyone who 

39
00:05:58,560 --> 00:06:04,480
has the skills we require. That includes 
Bob, who has what we need, but nothing more. 

40
00:06:04,480 --> 00:06:10,874
If I had to remove someone from our selection, Bob 
would be my choice. Let's see if Python agrees. 

41
00:06:10,874 --> 00:06:14,240
We checked if the candidates' skills 
are a superset of our required skills, 

42
00:06:14,240 --> 00:06:18,800
which means we also get people who's 
skill set is equal to our required skills. 

43
00:06:18,800 --> 00:06:23,040
If we check for a proper superset, instead, we 
should end up with people who have more than 

44
00:06:23,040 --> 00:06:28,400
what we require. That's not a bad criterion 
for reducing the list, so let's do it. 

45
00:06:28,400 --> 00:06:32,520
I'll change the test on line 
14. I won't delete that line - 

46
00:06:32,520 --> 00:06:37,256
I'll comment it out and add the new line below, 
so that you can compare the two bits of code: 

47
00:06:48,296 --> 00:06:52,800
required_skills is a list. I did that 
as a reminder that you can pass any 

48
00:06:52,800 --> 00:06:57,600
iterable to these set methods. You can 
see that in the commented out line 14. 

49
00:06:57,600 --> 00:07:02,240
So on line 15, I've had to 
convert required_skills to a set. 

50
00:07:02,240 --> 00:07:06,000
That's not very efficient, converting 
it each time round the loop. 

51
00:07:06,000 --> 00:07:09,680
In practice, you'd either convert 
it once, before the loop starts, 

52
00:07:09,680 --> 00:07:14,000
or create it as a set on line 1.
I don't want to change the code too much, 

53
00:07:14,000 --> 00:07:18,563
on video, so I'll leave it like that. Just 
be aware of the performance implications of 

54
00:07:18,563 --> 00:07:24,560
performing an operation like that, inside a loop, 
when you could do it once before the loop starts. 

55
00:07:24,560 --> 00:07:28,400
Alright, does it work ?
Run the program: 

56
00:07:29,800 --> 00:07:33,760
and we've eliminated Bob.
We used the proper superset operator, 

57
00:07:33,760 --> 00:07:38,880
greater than, on line 15. That only returns 
True if the skills are a proper superset of our 

58
00:07:38,880 --> 00:07:44,640
required skills – which means they must include at 
least one more skill than the required_skills set. 

59
00:07:44,640 --> 00:07:48,443
That demonstrates the difference 
between a subset (or superset) 

60
00:07:48,443 --> 00:07:53,680
and a proper subset (or superset), and gives 
an example of how they change the results. 

61
00:07:53,680 --> 00:07:56,720
And that's the end of our 
discussion on Python sets. 

62
00:07:56,720 --> 00:08:01,280
The next lecture is a document - a summary 
of everything we've covered in this section. 

63
00:08:01,280 --> 00:08:05,900
Make sure you're comfortable with all the topics 
covered, and I'll see you in the next section.

