WEBVTT

0
00:00.110 --> 00:05.750
Now, when you're working with lists, one of the most common errors you'll come across is something

1
00:05.750 --> 00:08.000
called the index out of range error.

2
00:08.030 --> 00:11.630
In fact, by this point, you might have already seen it.

3
00:11.810 --> 00:13.940
Now, what does it mean though?

4
00:14.030 --> 00:16.640
Well, let's take our states_of_america,

5
00:16.640 --> 00:20.330
and you might remember that there are 50 states in America,

6
00:20.330 --> 00:26.000
but if you don't, because you're a programmer, it's as easy as writing, len(),

7
00:26.000 --> 00:31.070
and then passing over the states_of_america, which will print 50.

8
00:31.100 --> 00:38.630
So now that we know that there are a total of 50 items in this list, and remember, because we start

9
00:38.630 --> 00:44.030
counting from 0, Hawaii is actually at index 49.

10
00:44.030 --> 00:51.500
So if we print states_of_america and then we try to get the item at 49, we hit print, you'll see that

11
00:51.500 --> 00:58.010
we get Hawaii printed. Now what if we went one beyond that?

12
00:58.010 --> 01:02.090
What if we tried to get the one at index 50?

13
01:02.090 --> 01:03.710
What do we get instead?

14
01:03.800 --> 01:05.870
Well, we get an error.

15
01:05.960 --> 01:07.850
It's called an IndexError.

16
01:07.850 --> 01:14.930
And this is because it's beyond Hawaii and there's nothing there, as we can see with our own eyes.

17
01:14.930 --> 01:21.000
But when you're working with large lists and you're not always looking at the data, then these errors

18
01:21.000 --> 01:23.460
can be a little bit more confusing.

19
01:23.520 --> 01:28.950
Very frequently when you're working with lists, you'll end up with an off-by-one error.

20
01:28.950 --> 01:36.630
So it's unusual that you'll try to get something at index number 90, because that's just way beyond

21
01:36.630 --> 01:37.740
your list size,

22
01:37.740 --> 01:44.280
but very frequently you might end up in a situation where you have some sort of value, say  

23
01:44.280 --> 01:46.590
number_of_states = len(),

24
01:46.590 --> 01:50.850
and then we pass over the states_of_america.

25
01:50.850 --> 01:53.310
So this is going to be equal to 50.

26
01:53.310 --> 01:59.730
And then we pass that inside here as the index num_of_states,

27
01:59.730 --> 02:01.290
and then we hit Run,

28
02:01.290 --> 02:03.510
and we get the same error, right?

29
02:03.510 --> 02:06.870
In this list. It's again list index out of range.

30
02:06.870 --> 02:14.280
And this is an off-by-one error because all we need to do is just simply -1,

31
02:14.280 --> 02:19.380
so that 1 becomes 0, and 50 becomes 49.

32
02:19.380 --> 02:21.510
And then we get rid of that error.

33
02:22.860 --> 02:27.060
Now it might be easier if we work with something a little bit simpler.

34
02:27.330 --> 02:35.190
Recently I was reading online, and I came across the so-called "Dirty Dozen," where the Environmental

35
02:35.190 --> 02:41.130
Working Group, a bunch of people crunched through a whole lot of data, probably using Python, and

36
02:41.130 --> 02:47.820
they released their Dirty Dozen, a list of the fruits and vegetables that have the most pesticides.

37
02:47.820 --> 02:54.900
And it's kind of crazy that they actually washed and peeled all of these foods and then tested them

38
02:54.900 --> 03:01.860
for pesticides. And the list looks something like this, where strawberries are apparently one of the

39
03:01.860 --> 03:03.870
worst offenders for pesticides.

40
03:03.870 --> 03:07.380
So let's create a list of the dirty_dozen.

41
03:07.380 --> 03:14.640
But you'll notice that some of these are fruits like strawberries, apples, and other ones are vegetables.

42
03:14.640 --> 03:21.510
So how can we use our lists to still keep them inside the same sort of container?

43
03:21.540 --> 03:26.790
The dirty_dozen, but somehow separate them out into fruits and vegetables?

44
03:27.720 --> 03:33.210
Well, we could just simply create two lists fruits and vegetables.

45
03:33.210 --> 03:36.870
But these two lists kind of have a relationship, right?

46
03:36.870 --> 03:42.270
They're kind of related because they're all on the list of high pesticide foods.

47
03:42.270 --> 03:46.200
So how can we have lists within a list?

48
03:46.230 --> 03:49.200
Well, that's what's called a nested list.

49
03:49.200 --> 03:57.060
Instead of our original dirty_dozen, we could create a new list called dirty_dozen, and we set it

50
03:57.060 --> 04:00.870
equal to a list that contains two lists.

51
04:00.870 --> 04:04.890
It contains fruits and it contains vegetables.

52
04:05.220 --> 04:13.480
So now what effectively has happened is we've inserted this list inside here, and then we've inserted

53
04:13.480 --> 04:16.060
this list inside here.

54
04:16.120 --> 04:20.710
So we now have a list that contains two lists.

55
04:21.070 --> 04:26.260
And if I go ahead and print out this list you'll be able to see its structure.

56
04:27.820 --> 04:29.530
And it looks like this.

57
04:29.560 --> 04:34.660
You'll notice that there's two brackets at the beginning and at the end.

58
04:34.660 --> 04:38.680
And the reason is because this is one list,

59
04:38.920 --> 04:42.490
this is another list,

60
04:42.490 --> 04:45.040
and this is also a list.

61
04:45.040 --> 04:52.780
So this is yet another way of using lists and just showing you the flexibility of this particular data

62
04:52.780 --> 04:53.620
structure.

63
04:53.830 --> 04:58.660
It's something that you're going to use a lot when you're writing Python code.