1
00:00:05,280 --> 00:00:10,080
I've created a new Python file for the next few 
videos. I've called this one modifying_sets.py 

2
00:00:12,240 --> 00:00:15,920
I'll start by typing some code that you 
might expect to create an empty set: 

3
00:00:20,560 --> 00:00:23,280
Ok, we've seen that sets use curly braces, 

4
00:00:23,280 --> 00:00:26,000
so that might look like a reasonable 
way to create an empty set. 

5
00:00:26,720 --> 00:00:30,720
But you can't do that. Python has no way 
to know if you want a set, or a dictionary. 

6
00:00:31,440 --> 00:00:36,000
Using opening and closing curly braces, as I've 
done there, will create an empty dictionary. 

7
00:00:36,800 --> 00:00:42,000
We can check that, by printing out the type of 
object that we've created. I'll do that on line 3: 

8
00:00:52,160 --> 00:00:59,280
Run the program, to see what type we get.
As you can see, numbers is a dict, not a set. 

9
00:01:00,000 --> 00:01:05,360
There is a way to create an empty set, using a 
literal, but it's horrible. Please don't do this! 

10
00:01:06,160 --> 00:01:09,920
I'll quickly demonstrate it, in case you 
come across it in code you have to work with. 

11
00:01:10,720 --> 00:01:15,200
Or in case it comes up in a job interview.
If you remember, from the discussion 

12
00:01:15,200 --> 00:01:19,440
of *args in the Functions section, you can 
use star to unpack a sequence or iterable. 

13
00:01:20,160 --> 00:01:24,560
Unpacking an empty sequence will produce no 
values, so we can use that to create an empty set. 

14
00:01:25,280 --> 00:01:26,400
The code looks like this:

15
00:01:35,520 --> 00:01:36,240
Run the program: 

16
00:01:38,640 --> 00:01:42,160
Now, numbers is a set.
As I said, avoid doing that. 

17
00:01:42,880 --> 00:01:46,720
Even very experienced Python programmers would 
take a while to work out what's happening there. 

18
00:01:47,440 --> 00:01:51,200
The only time you're likely to see something 
like that, is in a very tight loop, 

19
00:01:51,200 --> 00:01:54,320
where the programmer is trying to get every 
last bit of performance out of the code. 

20
00:01:55,120 --> 00:01:58,800
As we've seen, using a set literal is 
faster than calling the set function. 

21
00:01:59,600 --> 00:02:03,680
If you come across that in code, or in a job 
interview, you'll now know what it's doing. 

22
00:02:04,560 --> 00:02:09,360
I used an empty string there, but the same trick 
will work with a list, tuple or dictionary. 

23
00:02:10,080 --> 00:02:13,040
If your interviewer is being very 
sneaky, they might write that as:

24
00:02:21,840 --> 00:02:22,800
Run the program again. 

25
00:02:25,040 --> 00:02:31,040
We still get a set created, but the code is, 
probably, even harder to understand. But at 

26
00:02:31,040 --> 00:02:35,680
least you can impress your interviewer .
Alright, moving on. I'll change that 

27
00:02:35,680 --> 00:02:38,960
to the more usual way to create an 
empty set – using the set function: 

28
00:02:45,120 --> 00:02:48,080
Once again, when I run the 
program, numbers is a set. 

29
00:02:49,600 --> 00:02:55,040
To add items to a set, we use the add method. 
I'll type the code to do that, on line 5: 

30
00:03:06,960 --> 00:03:07,760
Run the program: 

31
00:03:10,000 --> 00:03:14,640
The integer value 1 is added to the set.
If you remember, sets can't contain 

32
00:03:14,640 --> 00:03:20,000
duplicates. We could take advantage of that, if 
we wanted to get 4 unique values from a user. 

33
00:03:20,640 --> 00:03:23,840
I'll comment out those 2 lines, 
and we'll add values inside a loop: 

34
00:04:00,880 --> 00:04:05,120
That's a very simple loop, that will terminate 
once our numbers set contains 4 items. 

35
00:04:05,920 --> 00:04:09,360
On line 10, we use the add method 
to add each new value to the set. 

36
00:04:10,160 --> 00:04:13,120
Of course, if you're getting 
integer values from your user, 

37
00:04:13,120 --> 00:04:16,560
you'd probably use something like the 
get_integer function that we wrote earlier. 

38
00:04:17,279 --> 00:04:21,279
I won't spend time, in this video, 
copying the function into this project. 

39
00:04:22,160 --> 00:04:25,120
Just make sure you only enter valid 
numbers, when we run this program. 

40
00:04:26,000 --> 00:04:30,320
Ok, I'll run it,
and enter 1 ENTER 

41
00:04:30,960 --> 00:04:32,880
2 ENTER
3 ENTER 

42
00:04:33,600 --> 00:04:36,320
4 ENTER
The loop terminates, 

43
00:04:36,320 --> 00:04:40,880
and we have a set containing the 4 values.
What happens if I enter the same value more than 

44
00:04:40,880 --> 00:04:47,200
once? Let's run the program again, to check.
This time, I'll use some different values: 

45
00:04:47,920 --> 00:04:49,840
12
34 

46
00:04:50,560 --> 00:04:52,400
56
12 again 

47
00:04:53,840 --> 00:05:00,080
Notice that the loop hasn't terminated.
34 again, and we're still looping. 

48
00:05:00,800 --> 00:05:05,280
12 once more. The code is 
still prompting for more input. 

49
00:05:06,080 --> 00:05:12,800
78 ENTER, and the loop terminates.
Our set contains 4 unique values, 

50
00:05:12,800 --> 00:05:16,800
the duplicates were ignored.
When I added 12 a second time, 

51
00:05:16,800 --> 00:05:23,120
it replaced the existing value 12, and the size of 
the set didn't increase. The code keeps looping, 

52
00:05:23,120 --> 00:05:26,960
until 4 unique values have been entered.
That can be a useful, 

53
00:05:26,960 --> 00:05:31,600
practical use for sets. They're an easy 
way to remove duplicate values from data. 

54
00:05:32,320 --> 00:05:36,560
We'll have a look at a couple of ways to do that; 
one way will use a set, and the other will use 

55
00:05:36,560 --> 00:05:42,560
the dictionary fromkeys method.
See you in the next video.

