1
00:00:04,689 --> 00:00:10,000
Alright, so let's talk about safely
removing values from a list. Alright, so

2
00:00:10,000 --> 00:00:14,230
we've seen how not to remove outlying
values from an ordered set of data, in

3
00:00:14,230 --> 00:00:19,000
the last video. Now let's have a look
at something that will work. Note that I

4
00:00:19,000 --> 00:00:24,260
said ordered data there. We'll see how to
delete items from an unsorted list next.

5
00:00:24,260 --> 00:00:28,440
This code will only work when the data
is sorted. I'm going to delete the

6
00:00:28,440 --> 00:00:33,620
commented out code, and all the code that
didn't work from the last video, first.

7
00:00:38,100 --> 00:00:43,360
Okay, so I'm going to assume that we
don't have enough memory to copy the list.

8
00:00:43,360 --> 00:00:46,800
I know our list is quite small but you
might be working with gigabytes of data

9
00:00:46,800 --> 00:00:51,000
in some scenarios. In that case, you may
not have enough RAM for copying the

10
00:00:51,010 --> 00:00:54,309
entire list. So what we're going to do is
start off by stripping off the low

11
00:00:54,309 --> 00:00:58,390
values from the start of the list. So
let's write some code for that.

12
00:00:58,390 --> 00:01:02,440
So line 7, I'm going to put a comment
here to tell us what we're doing:

13
00:01:02,440 --> 00:01:05,760
process the low values in the list.

14
00:01:07,800 --> 00:01:14,600
And we'll start coding. You want to make stop
equals zero, line 9, for index comma value

15
00:01:14,600 --> 00:01:20,560
in enumerate with data in
parentheses, colon, and we're gonna type

16
00:01:20,560 --> 00:01:28,880
if value is greater than equal to min_valid : stop equals index, break.

17
00:01:28,880 --> 00:01:35,590
Then moving on down, line 14, I'm
going to type print stop - that's for

18
00:01:35,590 --> 00:01:44,440
debugging purposes -
come back and over that, and del data,

19
00:01:44,440 --> 00:01:51,060
left square bracket, colon stop, right square
bracket and print data.

20
00:01:51,060 --> 00:01:55,120
Okay, so there's our code. So the approach is pretty straightforward

21
00:01:55,120 --> 00:02:01,090
We initialize stop to zero on line eight.
On line 15 we delete the slice from the

22
00:02:01,090 --> 00:02:06,520
beginning of the list, up to, but not
including, stop. Inside the loop we keep

23
00:02:06,520 --> 00:02:10,400
incrementing the stop value, until we find
a value that shouldn't be deleted. Remember

24
00:02:10,400 --> 00:02:15,760
that a slice extends up to, but not
including, the stop value. That means the

25
00:02:15,760 --> 00:02:22,150
value we've stopped out won't be deleted.
So let's run the program. As you can see,

26
00:02:22,150 --> 00:02:25,750
the first two values have been removed.
Stop has the value 2, as you can see

27
00:02:25,750 --> 00:02:30,380
from the output, and line 15 deleted the
slice from the start of the list, up to,

28
00:02:30,380 --> 00:02:36,000
but not including the value at position
2. The important thing here is that we

29
00:02:36,010 --> 00:02:40,720
don't change the data inside the loop.
It's only after the loop terminates, that

30
00:02:40,720 --> 00:02:46,030
we delete the slice from data. Alright, so
in the next video, we'll remove the high

31
00:02:46,030 --> 00:02:51,040
outlying values - that's the values 350
and 360 from the end of the list.

32
00:02:51,040 --> 00:02:54,060
See you in the next video.

