Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,250 --> 00:00:02,380
Now, when you're working with lists,
2
00:00:02,740 --> 00:00:06,760
one of the most common errors you'll come across is something called the index
3
00:00:06,790 --> 00:00:09,880
out of range error. In fact, by this point,
4
00:00:09,940 --> 00:00:14,560
you might have already seen it. Now, what does it mean though? Well,
5
00:00:14,560 --> 00:00:19,090
let's take all the states_of_america and let's delete the line where we added
6
00:00:19,300 --> 00:00:21,250
Angelaland and Jack Bauer land.
7
00:00:21,580 --> 00:00:26,580
So we go back to the original. And you might remember that there are 50 States in
8
00:00:26,950 --> 00:00:30,490
America. But if you don't because you are a programmer,
9
00:00:30,640 --> 00:00:35,640
it's as easy as writing len and then passing over the states_of_america
10
00:00:36,880 --> 00:00:38,140
which will print 50.
11
00:00:38,710 --> 00:00:43,710
So now that we know that there are a total of 50 items in this list,
12
00:00:44,560 --> 00:00:47,320
and remember, because we start counting from 0,
13
00:00:47,890 --> 00:00:51,190
Hawaii is actually at index 49.
14
00:00:51,580 --> 00:00:56,580
So if we print states_of_america and then we try to get the item at the 49,
15
00:00:57,070 --> 00:01:00,340
we hit print, you'll see that we get Hawaii printed.
16
00:01:01,660 --> 00:01:05,260
Now what if we went one beyond that?
17
00:01:05,320 --> 00:01:09,280
What if we tried to get the one at index 50,
18
00:01:09,460 --> 00:01:14,350
somewhere out here, what do we get instead? Well, we get an error.
19
00:01:14,860 --> 00:01:19,860
It's called an index error. And this is because it's beyond Hawaii and there's
20
00:01:20,650 --> 00:01:23,350
nothing there as we can see with our own eyes.
21
00:01:23,950 --> 00:01:27,790
But when you're working with large lists and you're not always looking at the
22
00:01:27,790 --> 00:01:28,623
data,
23
00:01:28,720 --> 00:01:33,720
then these errors can be a little bit more confusing. Very frequently when you're
24
00:01:33,880 --> 00:01:37,690
working with lists, you'll end up with an off by one error.
25
00:01:37,690 --> 00:01:42,690
So it's unusual that you'll try to get something at index number 90 because
26
00:01:43,120 --> 00:01:46,300
that's just way beyond your list size.
27
00:01:46,690 --> 00:01:51,640
But very frequently you might end up in a situation where you have some sort of
28
00:01:51,640 --> 00:01:55,330
value saying num_of_states = len
29
00:01:55,380 --> 00:01:59,500
and then we pass over the states_of_america.
30
00:01:59,740 --> 00:02:01,870
So this is going to be equal to 50.
31
00:02:02,290 --> 00:02:07,290
And then we pass that inside here as the index num of states.
32
00:02:08,620 --> 00:02:13,360
And then we hit run and we get the same error, right? On line 16
33
00:02:13,930 --> 00:02:18,190
where we try to get hold of this index in this list. It's again,
34
00:02:18,220 --> 00:02:19,540
list index out of range.
35
00:02:20,020 --> 00:02:25,020
And this is an off by one error because all we need to do is just simply minus
36
00:02:26,380 --> 00:02:27,213
1
37
00:02:27,400 --> 00:02:32,050
so that 1 becomes 0 and 50 becomes 49.
38
00:02:32,560 --> 00:02:34,180
And then we get rid of that error.
39
00:02:36,010 --> 00:02:39,730
Now it might be easier if we work with something a little bit simpler.
40
00:02:40,300 --> 00:02:45,300
Recently I was reading online and I came across the so-called Dirty Dozen where
41
00:02:47,320 --> 00:02:48,730
the environmental working group,
42
00:02:48,760 --> 00:02:53,050
a bunch of people, crunch through a whole lot of data, probably using Python.
43
00:02:53,530 --> 00:02:58,530
And they released their Dirty Dozen, a list of the fruits and vegetables that have
44
00:02:59,170 --> 00:03:04,170
most pesticides. And its kind of crazy that they actually washed and peeled all
45
00:03:05,650 --> 00:03:08,740
of these foods and then tested them for pesticides.
46
00:03:09,280 --> 00:03:14,280
And the list looks something like this where strawberries are apparently one of
47
00:03:14,650 --> 00:03:16,510
the worst offenders for pesticides.
48
00:03:17,080 --> 00:03:20,110
So let's create a list of the dirty dozen,
49
00:03:20,530 --> 00:03:24,640
but you'll notice that some of these are fruits like strawberries,
50
00:03:24,670 --> 00:03:27,460
apples, and the other ones are vegetables.
51
00:03:27,700 --> 00:03:32,700
So how can we use our lists to still keep them inside the same sort of
52
00:03:33,580 --> 00:03:35,650
container, the dirty dozen,
53
00:03:36,040 --> 00:03:39,460
but somehow separate them out into fruits and vegetables?
54
00:03:40,900 --> 00:03:44,500
Well, we could just simply create two lists,
55
00:03:44,530 --> 00:03:49,390
fruits and vegetables. But these two lists kind of have a relationship,
56
00:03:49,390 --> 00:03:49,780
right?
57
00:03:49,780 --> 00:03:54,780
That kind of related because they're all on the list of high-pesticide foods.
58
00:03:55,450 --> 00:04:00,160
So how can we have lists within a list? Well, that's,
59
00:04:00,190 --> 00:04:04,300
what's called a nested list. Instead of our original dirty dozen,
60
00:04:04,690 --> 00:04:08,500
we could create a new list called dirty_dozen,
61
00:04:08,950 --> 00:04:13,750
and we set it equal to a list that contains two lists.
62
00:04:13,930 --> 00:04:17,560
It contains fruits and it contains vegetables.
63
00:04:18,370 --> 00:04:23,370
So now what effectively has happened is we've inserted this list inside here and
64
00:04:25,480 --> 00:04:28,810
then we've inserted this list inside here.
65
00:04:29,290 --> 00:04:33,460
So we now have a list that contains two lists.
66
00:04:34,270 --> 00:04:38,980
And if I go ahead and print out this list, you'll be able to see its structure.
67
00:04:40,990 --> 00:04:42,160
And it looks like this.
68
00:04:42,760 --> 00:04:47,440
You'll notice that there's two brackets at the beginning and at the end
69
00:04:47,800 --> 00:04:51,340
and the reason is because this is one list,
70
00:04:52,210 --> 00:04:57,210
this is another list and this is also a list.
71
00:04:58,180 --> 00:05:03,180
So this is yet another way of using lists and showing you the flexibility of
72
00:05:04,420 --> 00:05:06,280
this particular data structure.
73
00:05:06,910 --> 00:05:11,380
It's something that you're going to use a lot when you're writing Python code.
74
00:05:12,520 --> 00:05:15,130
Now that we've talked more about index errors,
75
00:05:15,130 --> 00:05:18,040
how to fix them and also nested lists,
76
00:05:18,340 --> 00:05:21,070
you're ready to go to the next coding challenge.
77
00:05:21,460 --> 00:05:23,980
So head over there and give that a go.
6812
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.