1
00:00:04,939 --> 00:00:08,100
In this video, I want to talk about
removing items backwards.

2
00:01:03,340 --> 00:01:07,430
Let's see two ways to do that.
We need to create a new python

3
00:01:07,430 --> 00:01:18,860
file here. So I'm going to call this one
gobackwards.py.

4
00:01:18,860 --> 00:01:25,600
Let's start by creating some data, so data equals,
and in square brackets, because it's a list; 104

5
00:01:25,610 --> 00:01:35,930
comma 101 comma 4 comma 105 comma 308
comma 103 comma 5 comma, and I'm gonna

6
00:01:35,930 --> 00:01:42,900
advance to the next line,
107 comma 100 comma 306 comma 106 comma

7
00:01:42,900 --> 00:01:47,210
102 and the last one, 108. We've got
our right square bracket to close off

8
00:01:47,210 --> 00:01:53,400
the list definition. On line three I'm going
to type min_valid equals 100,

9
00:01:53,400 --> 00:01:59,680
line 5, line 4 rather, max_valid
is equal to 200. So we've got

10
00:01:59,689 --> 00:02:03,890
similar data to our earlier example but
it's not sorted. The values to remove

11
00:02:03,890 --> 00:02:11,030
appear randomly in the list, and it's 4 at
position 2, 308 at position 4, 5 appears

12
00:02:11,030 --> 00:02:17,150
at position 6 and the last one is 306 at
position 9. So we'll start off with a

13
00:02:17,150 --> 00:02:20,660
similar loop to the one we used in the
last program, and we'll print out the

14
00:02:20,660 --> 00:02:25,790
index numbers to check that we're getting
what we'd expect. So on line 6, for space

15
00:02:25,790 --> 00:02:33,900
index in range parenthesis len
parentheses data and right parenthesis,

16
00:02:33,900 --> 00:02:42,340
minus 1 comma minus 1 comma minus 1,
right parenthesis and a colon. And on line

17
00:02:42,349 --> 00:02:53,910
7, print parentheses index.
So if we run this program now, you can

18
00:02:53,910 --> 00:02:57,930
see we get the values from 12 down to
zero, which is what we want. We can safely

19
00:02:57,930 --> 00:03:01,709
remove the outlying values now, because
the values that we've still got to

20
00:03:01,709 --> 00:03:06,140
process aren't affected, when the items
shuffle down to fill in the gaps.

21
00:03:06,140 --> 00:03:09,680
And this'll be easier to understand if we
print out the list each time, so let's go

22
00:03:09,680 --> 00:03:14,620
ahead add some code. So I'm going to add
an if on line 7,

23
00:03:14,620 --> 00:03:21,360
if data left square bracket with index
in it, if that's less than min_valid

24
00:03:21,360 --> 00:03:31,130
or data index then square brackets,
is greater than max_valid:

25
00:03:31,130 --> 00:03:36,180
then we want print index to be indented.
As I mentioned, it'll be easier if we print

26
00:03:36,180 --> 00:03:41,970
them out each time, and then we'll
actually delete it, so del data left square

27
00:03:41,970 --> 00:03:45,420
brackets index. And what we might do,
rather than just printing the index,

28
00:03:45,420 --> 00:03:55,540
let's also print out the data as well; comma data. Alright. Let's run the program.

29
00:03:55,540 --> 00:04:00,420
You can see that the first line of
output has an index of 9. The value at

30
00:04:00,420 --> 00:04:06,000
position 9 is 306, which will be removed.
When the loop goes around again, we'll be

31
00:04:06,000 --> 00:04:11,660
processing position 8. The items in the
list before position 9 aren't affected

32
00:04:11,660 --> 00:04:16,579
when we delete 306. So that deletion will
work fine and won't mess up our loop.

33
00:04:16,579 --> 00:04:21,279
Next we get to index position 6. The item
there is 5 and that's correct - that's the

34
00:04:21,300 --> 00:04:28,240
next item to be deleted. 308 appears at
position 4, so that's also correct.

35
00:04:28,240 --> 00:04:33,270
Finally, we get the value 4 at position 2.
When that's deleted, we should have a

36
00:04:33,270 --> 00:04:36,660
list with all those outlying values
removed. So I should have printed out a

37
00:04:36,660 --> 00:04:43,860
final list, so that we can check that.
Let's add that to the code; print line 11

38
00:04:43,860 --> 00:04:50,970
parenthesis data. Run the code, and you
can see that that works. The four rogue

39
00:04:50,970 --> 00:04:55,289
values have gone from the list. This code
is a lot simpler than our previous

40
00:04:55,289 --> 00:04:59,920
example. If we ignore the diagnostic
output on line 8,

41
00:04:59,920 --> 00:05:03,200
our loop is only three lines of
code. Now this'll also work with a

42
00:05:03,210 --> 00:05:07,830
sorted list. Test it with all the test
data from the previous example, to

43
00:05:07,830 --> 00:05:11,430
confirm that it works. I won't do that in
the video, but remember to always

44
00:05:11,430 --> 00:05:15,380
consider edge and corner cases when
testing your code.

45
00:05:15,380 --> 00:05:21,080
Alright, so if this short loop does the
same job as all this, and I'll open up

46
00:05:21,080 --> 00:05:28,860
outliers.py again. So if it does the
same as all this code, which you can see

47
00:05:28,860 --> 00:05:33,240
obviously here, contains two loops, why did
I spend time writing that longer program.

48
00:05:33,240 --> 00:05:37,340
Surely shorter code is better. If there
are fewer lines of code, there's less

49
00:05:37,350 --> 00:05:41,160
chance of having a bug in the code. Now
there is a reason for this longer

50
00:05:41,160 --> 00:05:44,850
program. I'm going to keep you in
suspense a bit longer. We'll see why I

51
00:05:44,850 --> 00:05:48,840
wrote all that, in the video after the
next one. In the next video, however,

52
00:05:48,840 --> 00:05:54,560
I want to show you another way to iterate
backwards. See you in the next video.

