1
00:00:04,720 --> 00:00:09,580
Alright, in this video I want to look at
the reversed function. So we'll continue

2
00:00:09,580 --> 00:00:13,210
with gobackwards.py but we're going
to use a different loop, to iterate

3
00:00:13,210 --> 00:00:18,310
backwards over our lists. This time I'll
use the reversed function. So let's see

4
00:00:18,310 --> 00:00:21,820
what the documentation has to say about it.

5
00:00:21,820 --> 00:00:26,400
So I'm going to come down here and click on
reversed. So the reversed function returns

6
00:00:26,400 --> 00:00:31,380
a reverse iterator. An iterator is what
takes care of iterating over something.

7
00:00:31,380 --> 00:00:35,860
When you use a for loop, Python creates
an iterator for you, and that's why

8
00:00:35,860 --> 00:00:39,940
modifying the loop control variable
doesn't work, by the way. The iterator is

9
00:00:39,940 --> 00:00:43,360
keeping track of where it was in the
sequence and resets the loop control

10
00:00:43,360 --> 00:00:47,739
variable, when the next item is requested.
I don't want to complicate things by

11
00:00:47,739 --> 00:00:52,030
talking about iterators in detail, just
yet. This documentation is confusing

12
00:00:52,030 --> 00:00:55,539
enough. It talks about underscore
underscore reversed underscore

13
00:00:55,540 --> 00:01:00,900
underscore methods, and get item with
integer arguments starting at zero.

14
00:01:00,900 --> 00:01:04,899
It makes no sense.
Don't worry though, it will. At the moment,

15
00:01:04,899 --> 00:01:08,830
all we're interested in, is that this
function gives us a way to iterate over

16
00:01:08,830 --> 00:01:13,840
a sequence in reverse. Anything that
supports the sequence protocol is a

17
00:01:13,840 --> 00:01:20,259
sequence, and our lists our sequences. So
back to our code. Let's see how to use

18
00:01:20,259 --> 00:01:24,910
reversed when looping. The advantage of
using reversed is that it lets us

19
00:01:24,910 --> 00:01:29,440
use enumerate. So what I'm going to do is
comment out this code from line six

20
00:01:29,440 --> 00:01:37,569
through nine. Then line 10, well actually
line 11, I'm going to type for index comma

21
00:01:37,569 --> 00:01:45,910
value in enumerate left parenthesis
reversed left parenthesis data, and our

22
00:01:45,910 --> 00:01:50,319
two right parentheses and a colon. And
let's now print out index and value to

23
00:01:50,319 --> 00:01:55,450
see what we're getting.
So print parentheses index comma value

24
00:01:55,450 --> 00:01:59,700
right parenthesis, and let's run the
program.

25
00:02:02,040 --> 00:02:06,630
You can see we're getting the items in
reverse order - 108 appears first in the

26
00:02:06,630 --> 00:02:11,250
output. That's at index position zero. You
may not have expected that because it's

27
00:02:11,250 --> 00:02:17,310
at position 12 in the original list, up
here on lines one and two. When you use a

28
00:02:17,310 --> 00:02:23,120
reversed iterator, the index positions
relate to the data in reverse order.

29
00:02:23,120 --> 00:02:27,680
The indexes, in other words, still start at
zero. The last item we get, looking at

30
00:02:27,690 --> 00:02:31,950
the list down there, right down the
bottom there, line, the output 12, or

31
00:02:31,950 --> 00:02:37,290
index position 12, and the value of 104.
It's easy to get the correct index

32
00:02:37,290 --> 00:02:42,020
positions. All we need to do is just
subtract index from 12, in this case.

33
00:02:42,020 --> 00:02:46,440
We want our code to work with different
lists, so we won't use the literal value 12.

34
00:02:46,440 --> 00:02:50,640
Instead, we'll use one less than the
length of the list. So let's write some

35
00:02:50,640 --> 00:02:57,150
code for that. So up on line 11, I'm
going to start by typing top underscore

36
00:02:57,150 --> 00:03:04,290
index is equal to len parentheses data
minus 1. Now that we've done that,

37
00:03:04,290 --> 00:03:10,410
on our print out, here on line 13, we can
type top_index minus index,

38
00:03:10,410 --> 00:03:16,010
instead of just index. Run the program,

39
00:03:16,340 --> 00:03:20,850
and this time we get the real index
positions of each value in our list, on

40
00:03:20,850 --> 00:03:24,780
lines 1 & 2. The rest of the code is as
we've used before, so I'm going to just

41
00:03:24,780 --> 00:03:27,660
type that in quickly.
We'll leave the print in, but we need to

42
00:03:27,660 --> 00:03:36,690
add an if up here; if value is less then
min_valid, or value is

43
00:03:36,690 --> 00:03:42,560
greater than max_valid colon.
Then we want to indent our print.

44
00:03:42,560 --> 00:03:47,640
Then we also want to delete the data on
the next line, so del space data, and it's

45
00:03:47,640 --> 00:03:57,960
going to be top_index minus
index. Alright so I'll run the program,

46
00:03:57,960 --> 00:04:02,760
and we see the values that will be removed and
then the final list. Now once again,

47
00:04:02,760 --> 00:04:07,500
you should test this properly. Copy the data
from outliers.py and test with each

48
00:04:07,500 --> 00:04:11,140
one of those lists. It does work but
don't take my word for it.

49
00:04:11,140 --> 00:04:15,680
Test it yourself to be sure. If you get a problem,
check your code carefully.

50
00:04:15,680 --> 00:04:21,120
Alright, so that's two ways to iterate
backwards over a sequence.

51
00:04:21,120 --> 00:04:25,640
I think the second method is actually
easier to understand. It's pretty obvious

52
00:04:25,650 --> 00:04:30,900
that we were reversing the list on line 12.
It's harder to work out, though, what line

53
00:04:30,900 --> 00:04:34,980
six is doing, without a bit of thought.
The second way is also slightly more

54
00:04:34,980 --> 00:04:39,870
efficient, once you get above 1,000 items
in the list. Using enumerate is more

55
00:04:39,870 --> 00:04:44,760
efficient than those index lookups on
line seven. That brings me nicely to the

56
00:04:44,760 --> 00:04:49,200
question I asked earlier. Both of those
loops are only three lines long, if you

57
00:04:49,200 --> 00:04:53,490
ignore the diagnostic print lines. Both
of them also work well with sorted data,

58
00:04:53,490 --> 00:04:58,320
as well. So switching back to
outliers.py, why did I write all this

59
00:04:58,320 --> 00:05:03,380
code earlier. We'll see why in the next
video.

