1
00:00:05,360 --> 00:00:07,680
In this video, we'll have 
a look at the pop method. 

2
00:00:09,840 --> 00:00:13,360
We've seen pop used with a dictionary, 
and it can also be used with a list. 

3
00:00:14,240 --> 00:00:17,594
When popping items from a set, 
there's a slight difference. 

4
00:00:17,760 --> 00:00:21,972
Sets aren't indexable, so the set pop 
method doesn't take any arguments. 

5
00:00:22,160 --> 00:00:26,239
It pops an arbitrary item from 
the set, and returns that item. 

6
00:00:28,640 --> 00:00:32,720
What do I mean by "arbitrary" item?
I mean that you can't reliably predict 

7
00:00:32,720 --> 00:00:36,800
which item will be popped from the set.
You might get consistent behaviour, 

8
00:00:36,800 --> 00:00:40,819
when popping integer values from a 
set, with some versions of Python. 

9
00:00:40,880 --> 00:00:46,019
But if you run the same code on another Python 
version, you might get different values. 

10
00:00:48,160 --> 00:00:51,920
You may think that getting "random" 
items from your set isn't much use. 

11
00:00:51,920 --> 00:00:56,165
How can you write reliable code, if you don't 
know which value you'll be dealing with? 

12
00:00:56,320 --> 00:00:59,611
In fact, you'll find that you often don't care. 

13
00:01:01,760 --> 00:01:06,532
Imagine you're processing a set of tasks, and 
it doesn't matter what order they're performed. 

14
00:01:06,640 --> 00:01:10,165
Popping them from a set 
would be fine, in that case. 

15
00:01:10,165 --> 00:01:15,040
The pip module works like that. You can see 
a reference to it, in Martijn Pieter's answer 

16
00:01:15,040 --> 00:01:20,431
to this stack overflow question.
I'll switch to it in my browser. 

17
00:01:24,240 --> 00:01:29,351
Read through it at your leisure. The link 
will be in the resources for this video. 

18
00:01:31,120 --> 00:01:34,212
We're going to do something 
similar, in our example. 

19
00:01:34,320 --> 00:01:38,480
We'll process each of the patients in 
our trial – from the previous video. 

20
00:01:38,480 --> 00:01:42,080
We don't care what order we process 
them; as long as we process them all, 

21
00:01:42,080 --> 00:01:47,040
and each one only gets processed once.
Ok, let's go back to IntelliJ, 

22
00:01:47,040 --> 00:01:50,160
and see pop in action.
I've created a new Python 

23
00:01:50,160 --> 00:01:55,792
file called prescription_processing.py - 
pause the video while you catch up. 

24
00:01:56,080 --> 00:01:59,478
Start with the code to 
create the trial_patients set 

25
00:02:18,720 --> 00:02:22,720
Our code is going to be fairly simple. As 
long as there are patients remaining in 

26
00:02:22,720 --> 00:02:28,080
the trial_patients set, we'll loop, and 
process their prescription in some way. 

27
00:02:28,080 --> 00:02:31,897
To do that, we'll loop, as 
long as the set isn't empty: 

28
00:02:35,840 --> 00:02:40,195
Inside the loop, we'll pop a patient 
from the set, and print their name: 

29
00:02:53,520 --> 00:02:56,000
Okay, run the program. 

30
00:02:57,200 --> 00:03:01,406
It's not terribly exciting, we get 
the name of each patient printed out. 

31
00:03:01,406 --> 00:03:04,916
Each time you run it, you'll probably 
get the names in a different order. 

32
00:03:05,040 --> 00:03:09,776
That's what the documentation means by "Remove 
and return an arbitrary element from the set.". 

33
00:03:09,840 --> 00:03:13,895
Sets have no ordering, remember! .
So that's the pop method. 

34
00:03:14,000 --> 00:03:18,323
It removes an item from the set, and returns 
it so that you can do something with it. 

35
00:03:18,560 --> 00:03:23,644
We could, for example, use this code to send 
the patients' prescriptions to the pharmacy. 

36
00:03:23,760 --> 00:03:28,800
I'll start by importing the patients 
dictionary, from the prescription_data file. 

37
00:03:28,800 --> 00:03:30,825
I'll do that on line 1:

38
00:03:37,503 --> 00:03:43,284
Inside the loop, we can look up the patient's prescription in the dictionary.

39
00:04:03,870 --> 00:04:07,632
Each time we run the program, which I'll do now:

40
00:04:08,400 --> 00:04:12,628
we get the patient names, and 
their prescriptions, printed out. 

41
00:04:12,880 --> 00:04:16,720
Elsewhere in the code, we might be 
adding more patients to the trial. 

42
00:04:16,720 --> 00:04:20,959
This code ensures that we don't produce 
prescriptions for the same patient again. 

43
00:04:20,959 --> 00:04:24,320
They're removed from the set, 
and won't be processed again. 

44
00:04:24,320 --> 00:04:29,260
That's the technique used in the distlib 
library, mentioned in that stack overflow post. 

45
00:04:29,360 --> 00:04:33,376
pop probably isn't something you'll use 
very often, but it's there when you need it. 

46
00:04:33,520 --> 00:04:41,440
Alright, in the next video, we'll start to look 
at the set operations. See you in the next video.

