WEBVTT

1
00:00:00.000 --> 00:00:01.010
<v ->Welcome back.</v>

2
00:00:01.010 --> 00:00:02.100 line:15% 
In this video, we're going to look

3
00:00:02.100 --> 00:00:04.240 line:15% 
at some advanced set operations,

4
00:00:04.240 --> 00:00:09.240
which is why sets are useful compared to lists and tuples.

5
00:00:09.260 --> 00:00:11.400
So what I've got here is my art_friends

6
00:00:11.400 --> 00:00:12.640
and my science_friends.

7
00:00:12.640 --> 00:00:16.000
In arts, we've got Rolf, Anne and Jen.

8
00:00:16.000 --> 00:00:18.140
And in science, we've got Jen and Charlie.

9
00:00:18.140 --> 00:00:21.400
So a couple of things you can notice about these sets

10
00:00:21.400 --> 00:00:25.260
is that Jen does both art and science, right?

11
00:00:25.260 --> 00:00:26.780
She's in both sets.

12
00:00:26.780 --> 00:00:30.040
Rolf and Anne do art but not science.

13
00:00:30.040 --> 00:00:32.890
And Charlie does science but not art.

14
00:00:32.890 --> 00:00:34.880
And in total, we have four people,

15
00:00:34.880 --> 00:00:37.863
Rolf, Anne, Jen and Charlie.

16
00:00:38.790 --> 00:00:42.100
So we can use sets to extract that information

17
00:00:42.100 --> 00:00:43.770
from this data.

18
00:00:43.770 --> 00:00:44.860
Here's how.

19
00:00:44.860 --> 00:00:48.193
Let's get those people that do art but not science.

20
00:00:48.193 --> 00:00:50.987
So we'll say art_but_not_science.

21
00:00:50.987 --> 00:00:52.016
And what you want to do

22
00:00:52.016 --> 00:00:56.965
is you wanna say art_friends.difference(science_friends).

23
00:00:56.965 --> 00:00:59.530
So the difference gives you which elements

24
00:00:59.530 --> 00:01:02.773
are in art that are not in science.

25
00:01:02.773 --> 00:01:06.830
So the ones that are in art but not in science

26
00:01:06.830 --> 00:01:09.820
means that it's gonna do Rolf, Anne and Jen,

27
00:01:09.820 --> 00:01:12.970
it's gonna take away all the elements that are here

28
00:01:12.970 --> 00:01:13.920
that are also here.

29
00:01:13.920 --> 00:01:17.370
So in this case, Jen is both, so it's gonna get taken away

30
00:01:17.370 --> 00:01:19.833
and what you'll get back is Rolf and Anne.

31
00:01:21.000 --> 00:01:23.357
Of course you can do science_but_not_art

32
00:01:24.460 --> 00:01:25.880
and it's just the opposite

33
00:01:25.880 --> 00:01:27.880
science_friends.difference(art_friends).

34
00:01:29.640 --> 00:01:31.010
And what that's gonna do is it's gonna take

35
00:01:31.010 --> 00:01:32.870
your science friends, Jen and Charlie

36
00:01:32.870 --> 00:01:36.880
and it's gonna take away those that are in art, so Jen.

37
00:01:36.880 --> 00:01:39.640
And it's gonna leave you just with Charlie.

38
00:01:39.640 --> 00:01:40.740
Let's print those out.

39
00:01:42.210 --> 00:01:43.560
And you'll see what I mean.

40
00:01:44.520 --> 00:01:47.260
So in the first one you have Anne and Rolf

41
00:01:47.260 --> 00:01:48.520
but not Jen.

42
00:01:48.520 --> 00:01:51.510
And in the second one you have Charlie but not Jen.

43
00:01:51.510 --> 00:01:52.960
Sometimes what you want is something

44
00:01:52.960 --> 00:01:55.100
called the symmetric difference,

45
00:01:55.100 --> 00:01:58.920
which is members that aren't in both sets.

46
00:01:58.920 --> 00:02:02.380
So what members are not in both sets?

47
00:02:02.380 --> 00:02:04.213
Rolf, Anne and Charlie.

48
00:02:05.110 --> 00:02:07.190
So let's get that.

49
00:02:07.190 --> 00:02:09.240
So not_in_both

50
00:02:09.240 --> 00:02:12.567
will be art_friends.symmetric_difference(science_friends).

51
00:02:17.780 --> 00:02:20.350
So you get Anne, Rolf and Charlie.

52
00:02:20.350 --> 00:02:23.720
So those are the members that are not in both.

53
00:02:23.720 --> 00:02:26.090
Since Jen is the only one that's in both,

54
00:02:26.090 --> 00:02:28.890
what you end up is everything together

55
00:02:28.890 --> 00:02:31.510
except the ones that are in both.

56
00:02:31.510 --> 00:02:33.630
Of course because it says symmetric difference,

57
00:02:33.630 --> 00:02:35.610
it doesn't matter which one goes first.

58
00:02:35.610 --> 00:02:36.621
You can put

59
00:02:36.621 --> 00:02:38.620
science_friends.symmetric_difference, art_friends

60
00:02:38.620 --> 00:02:40.155
or you can do what we've done here,

61
00:02:40.155 --> 00:02:43.280
art_friends.symmetric_difference(science_friends).

62
00:02:43.280 --> 00:02:46.090
So to recap, difference does elements

63
00:02:46.090 --> 00:02:48.400
that are in one but not the other,

64
00:02:48.400 --> 00:02:50.380
where the symmetric difference does elements

65
00:02:50.380 --> 00:02:52.200
that are not in both.

66
00:02:52.200 --> 00:02:55.320
If you wanted the members that are in both sets,

67
00:02:55.320 --> 00:02:57.850
so Jen, you can do that

68
00:02:57.850 --> 00:02:59.490
with something called the intersection.

69
00:02:59.490 --> 00:03:02.020
So you could do who is doing art and science,

70
00:03:02.020 --> 00:03:07.020
art_and_science and that would be art_friends.intersection

71
00:03:07.520 --> 00:03:09.217
with science_friends.

72
00:03:10.860 --> 00:03:14.800
And what that would give you is just Jen in her own set.

73
00:03:14.800 --> 00:03:16.720
Notice that this is still a set

74
00:03:16.720 --> 00:03:19.100
because it has the curly braces around it.

75
00:03:19.100 --> 00:03:21.453
So that is a set of one element.

76
00:03:22.930 --> 00:03:25.120
Finally, if you wanted all the people

77
00:03:25.120 --> 00:03:28.820
that are studying any subject, but without duplicates,

78
00:03:28.820 --> 00:03:33.820
you could do all_friends is art_friends.union

79
00:03:34.430 --> 00:03:36.080
with science_friends.

80
00:03:36.080 --> 00:03:38.140
And that is gonna unite both sets

81
00:03:38.140 --> 00:03:40.793
and give you one big set with everybody in it.

82
00:03:42.300 --> 00:03:44.730
Of course, note that Jen is there twice,

83
00:03:44.730 --> 00:03:48.020
so it's only gonna appear once in the final set

84
00:03:48.020 --> 00:03:49.870
because sets cannot contain duplicates

85
00:03:49.870 --> 00:03:51.740
and you can see that this is still a set

86
00:03:51.740 --> 00:03:53.390
so there are no duplicates in it.

87
00:03:54.500 --> 00:03:58.550
These operations are why sets are useful.

88
00:03:58.550 --> 00:04:02.540
For example, you could have a lottery game

89
00:04:02.540 --> 00:04:05.160
where your user has a few numbers

90
00:04:05.160 --> 00:04:08.150
and the system has decided on six numbers

91
00:04:08.150 --> 00:04:10.570
that are the lottery winners.

92
00:04:10.570 --> 00:04:13.620
By having them both on sets, you could calculate

93
00:04:13.620 --> 00:04:15.000
the intersection of them two,

94
00:04:15.000 --> 00:04:16.710
which elements the user has matched

95
00:04:16.710 --> 00:04:18.850
with the winning elements

96
00:04:18.850 --> 00:04:21.520
and then calculate the user's winnings like that.

97
00:04:21.520 --> 00:04:23.950
And that is, in fact, going to be one of the challenges

98
00:04:23.950 --> 00:04:25.540
for this section.

99
00:04:25.540 --> 00:04:28.050
If you're not completely sure when to use

100
00:04:28.050 --> 00:04:31.820
each of these data structures, lists, tuples and sets,

101
00:04:31.820 --> 00:04:33.130
don't worry too much,

102
00:04:33.130 --> 00:04:35.310
over time you will get better at it.

103
00:04:35.310 --> 00:04:39.120
And most of the time, you'll be using tuples and lists.

104
00:04:39.120 --> 00:04:42.793
Sets are reserved for when you want to use these operations.

105
00:04:43.700 --> 00:04:45.000
Thanks for joining me in this video,

106
00:04:45.000 --> 00:04:46.060
I hope you've learned something,

107
00:04:46.060 --> 00:04:47.613 line:15% 
and I'll see you in the next one.

