1
00:00:05,230 --> 00:00:09,160
Deleting the items from the end of the list
needs a bit more thought.

2
00:00:09,160 --> 00:00:13,700
If there are billions of items, we don't to
work through the entire list to find the first

3
00:00:13,700 --> 00:00:15,110
value to be deleted.

4
00:00:15,110 --> 00:00:18,400
Instead, we can process the list from the end.

5
00:00:18,960 --> 00:00:20,180
So let's have a go at doing that.

6
00:00:20,420 --> 00:00:30,520
On line 18 I'm going to type a comment; we're going to process the high values in the list.

7
00:00:30,520 --> 00:00:34,900
We're going to type start is equal to zero.

8
00:00:36,180 --> 00:00:39,140
On line 19, we're initializing start to zero.

9
00:00:39,140 --> 00:00:40,740
Next, we need a for loop.

10
00:00:40,740 --> 00:00:42,060
And we want to work backwards,

11
00:00:42,060 --> 00:00:45,040
which means that we'll start at the index position 1 less

12
00:00:45,140 --> 00:00:46,800
than the length of the list.

13
00:00:46,800 --> 00:00:57,520
So I'm going to type for index in range
parenthesis len parenthesis data right parenthesis

14
00:00:57,530 --> 00:01:01,140
minus 1 comma.

15
00:01:01,140 --> 00:01:04,089
Now we can't use 0 as the stop value.

16
00:01:04,089 --> 00:01:06,660
We discussed that in the videos on indexing.

17
00:01:06,660 --> 00:01:11,320
Remember that the last value in a range or slice isn't included.

18
00:01:11,320 --> 00:01:15,140
If we use 0, the first item in the list won't
be included.

19
00:01:15,240 --> 00:01:17,840
And that's why we're using -1 as the stop value.

20
00:01:18,820 --> 00:01:20,740
I'm going to go ahead and put -1 there.

21
00:01:20,820 --> 00:01:26,560
And finally, to work backwards, we use a step of -1, so I'm going to do that.

22
00:01:26,560 --> 00:01:28,660
right parenthesis and a colon.

23
00:01:28,660 --> 00:01:34,320
When you're creating ranges like this, I suggest
you print out the values that your range produces.

24
00:01:34,330 --> 00:01:36,400
When you're starting out with all this, it
can be very confusing.

25
00:01:36,400 --> 00:01:40,540
Heck, when you've been doing it for years,
it can still be confusing.

26
00:01:40,540 --> 00:01:43,810
So let's make sure we're getting the index
numbers we want.

27
00:01:43,810 --> 00:01:49,290
So there are 16 items left in our list, so
we want to get the values from 15 to 0.

28
00:01:49,290 --> 00:01:53,920
So what I'll do is I'll print out index to confirm
that our range is correct.

29
00:01:54,440 --> 00:01:59,780
I said 16 there because the earlier code
has deleted the first two items.

30
00:01:59,780 --> 00:02:02,220
Our list started off with 18 items,  but by the

31
00:02:02,220 --> 00:02:06,620
time we get to this code that we're writing, it'll contain only 16 items.

32
00:02:06,620 --> 00:02:09,180
Let me go ahead and print(index)

33
00:02:14,540 --> 00:02:18,480
Then we'll run this

34
00:02:21,800 --> 00:02:25,460
You can see that's working, we've got 0 to 15 being output.

35
00:02:25,860 --> 00:02:31,099
Alright, so we're going to be iterating backwards through the list, from 15 to 0.

36
00:02:31,099 --> 00:02:35,409
If we find a value that's within the valid
range - so a value that's less than or equal

37
00:02:35,409 --> 00:02:38,500
to 200 - we can break out of the loop.

38
00:02:38,500 --> 00:02:40,340
So let's look at doing that.

39
00:02:40,340 --> 00:02:43,519
I'll delete the printout, and change the code to be

40
00:02:43,519 --> 00:02:48,650
if data left square brackets, index within those brackets,

41
00:02:48,650 --> 00:02:53,800
that's less than or equal to max underscore valid

42
00:02:54,020 --> 00:02:59,860
then a colon, then on the next line, start is equal to index, and then we're going to break.

43
00:03:01,620 --> 00:03:04,300
This gives us a start value that's equal to

44
00:03:04,310 --> 00:03:07,400
the position of the last item that we want
to keep.

45
00:03:07,400 --> 00:03:08,930
So with our data,

46
00:03:08,930 --> 00:03:10,250
that should be 13.

47
00:03:10,250 --> 00:03:14,359
The last value we want is 191, and that's at index position

48
00:03:14,359 --> 00:03:16,189
13.

49
00:03:16,189 --> 00:03:18,519
So we'll print out start to confirm
it's what we expect.

50
00:03:19,520 --> 00:03:26,220
So print start, in parentheses, and a comment there for debugging.

51
00:03:26,420 --> 00:03:30,480
We'll run this, to confirm that we get 13.

52
00:03:30,480 --> 00:03:33,020
We got 13, so that's working fine.

53
00:03:33,020 --> 00:03:37,930
Next, we want to delete the slice that includes the values at the end of the list.

54
00:03:37,930 --> 00:03:39,960
Let's go ahead and write the code for that.

55
00:03:42,020 --> 00:03:48,480
del data left square bracket, start colon within the square brackets.

56
00:03:48,480 --> 00:03:51,980
Let's print data after that, as well.

57
00:03:51,980 --> 00:03:56,960
So our slice on line 26 includes everything
from the start value to the end of the list.

58
00:03:56,960 --> 00:04:00,020
So we're going to delete everything from position 13 to the end.

59
00:04:00,020 --> 00:04:04,310
Now that, by the way, will include the value 191, which isn't what we want.

60
00:04:04,310 --> 00:04:10,060
For our data, we want to start deleting at
position 14, not from 13.

61
00:04:10,060 --> 00:04:12,700
So what we we need to do is add one to the start value.

62
00:04:12,700 --> 00:04:14,260
And we can do that on line 26.

63
00:04:14,260 --> 00:04:19,529
Come up here and type start plus one.

64
00:04:19,529 --> 00:04:21,880
That'll work - our slice will now start at
position 14.

65
00:04:21,880 --> 00:04:26,290
Now we can do the same thing by changing line
22 instead.

66
00:04:26,290 --> 00:04:28,160
So I'm going to undo the change I've just made.

67
00:04:28,520 --> 00:04:35,620
So let's put the change, or modify, line 22, to be start = index + 1

68
00:04:36,380 --> 00:04:39,380
Now that does the same thing, but the code's a bit cleaner.

69
00:04:39,740 --> 00:04:42,280
As far as the computer's concerned, there's no difference.

70
00:04:42,520 --> 00:04:48,140
We'll still be removing all the items from
position 14. However, the code's safer for humans

71
00:04:48,140 --> 00:04:49,170
to work with.

72
00:04:49,170 --> 00:04:52,670
The purpose of our for loop is to identify
the first item to delete.

73
00:04:52,670 --> 00:04:58,440
It sets start to the position to start deleting from, rather than from the position of the last

74
00:04:58,440 --> 00:05:02,020
item we want to keep, so that just makes more sense.

75
00:05:02,020 --> 00:05:04,780
This is the sort of thing that comes with
experience.

76
00:05:04,780 --> 00:05:10,190
Both versions would work fine, but it's cleaner
to have the correct start position when the

77
00:05:10,190 --> 00:05:11,420
loop terminates.

78
00:05:11,420 --> 00:05:15,380
Having to alter start, after the loop, is
a bit clumsy and it is something that's easy

79
00:05:15,380 --> 00:05:16,380
to forget.

80
00:05:16,380 --> 00:05:18,690
But don't worry too much about that.

81
00:05:18,690 --> 00:05:21,546
When you're starting, you'll be pleased to just get code that works.

82
00:05:21,546 --> 00:05:22,260
You'll understand

83
00:05:22,260 --> 00:05:26,680
why this version's slightly easier to work
with, when we've looked at writing our own functions.

84
00:05:26,680 --> 00:05:28,520
What I probably should do,

85
00:05:28,520 --> 00:05:32,120
is add a comment to explain why we're adding 1 to the value.

86
00:05:32,120 --> 00:05:36,560
Let's go ahead and do that, we'll do it right under the if statement, on line 21.

87
00:05:37,100 --> 00:05:45,320
So basically we have the index of the last item to keep

88
00:05:45,580 --> 00:05:50,000
So we want to set start to the position of
the first item to delete, which is one after

89
00:05:50,000 --> 00:05:50,620
index.

90
00:05:50,620 --> 00:05:52,180
Let's just type that.

91
00:06:01,020 --> 00:06:03,760
and that's one after index.

92
00:06:04,420 --> 00:06:07,500
I'm putting the variable names in single quotes there, in the comments.

93
00:06:08,120 --> 00:06:12,540
Alright, so now that I've done that, let's see if it works. I'm going to run the program

94
00:06:14,700 --> 00:06:17,240
And you can see that the list no longer contains the first two

95
00:06:17,240 --> 00:06:20,570
values, and the last two values have also been stripped out.

96
00:06:20,570 --> 00:06:24,330
So that seems to be working fine
- at least it's working fine with this particular

97
00:06:24,330 --> 00:06:25,330
set of data.

98
00:06:25,330 --> 00:06:29,270
In the next video, we'll look at testing it
more thoroughly.

99
00:06:29,270 --> 00:06:30,169
See you in the next video.

