Now, when you're working with lists,
one of the most common errors you'll come across is something called the index
out of range error. In fact, by this point,
you might have already seen it. Now, what does it mean though? Well,
let's take all the states_of_america and let's delete the line where we added
Angelaland and Jack Bauer land.
So we go back to the original. And you might remember that there are 50 States in
America. But if you don't because you are a programmer,
it's as easy as writing len and then passing over the states_of_america
which will print 50.
So now that we know that there are a total of 50 items in this list,
and remember, because we start counting from 0,
Hawaii is actually at index 49.
So if we print states_of_america and then we try to get the item at the 49,
we hit print, you'll see that we get Hawaii printed.
Now what if we went one beyond that?
What if we tried to get the one at index 50,
somewhere out here, what do we get instead? Well, we get an error.
It's called an index error. And this is because it's beyond Hawaii and there's
nothing there as we can see with our own eyes.
But when you're working with large lists and you're not always looking at the
data,
then these errors can be a little bit more confusing. Very frequently when you're
working with lists, you'll end up with an off by one error.
So it's unusual that you'll try to get something at index number 90 because
that's just way beyond your list size.
But very frequently you might end up in a situation where you have some sort of
value saying num_of_states = len
and then we pass over the states_of_america.
So this is going to be equal to 50.
And then we pass that inside here as the index num of states.
And then we hit run and we get the same error, right? On line 16
where we try to get hold of this index in this list. It's again,
list index out of range.
And this is an off by one error because all we need to do is just simply minus
1
so that 1 becomes 0 and 50 becomes 49.
And then we get rid of that error.
Now it might be easier if we work with something a little bit simpler.
Recently I was reading online and I came across the so-called Dirty Dozen where
the environmental working group,
a bunch of people, crunch through a whole lot of data, probably using Python.
And they released their Dirty Dozen, a list of the fruits and vegetables that have
most pesticides. And its kind of crazy that they actually washed and peeled all
of these foods and then tested them for pesticides.
And the list looks something like this where strawberries are apparently one of
the worst offenders for pesticides.
So let's create a list of the dirty dozen,
but you'll notice that some of these are fruits like strawberries,
apples, and the other ones are vegetables.
So how can we use our lists to still keep them inside the same sort of
container, the dirty dozen,
but somehow separate them out into fruits and vegetables?
Well, we could just simply create two lists,
fruits and vegetables. But these two lists kind of have a relationship,
right?
That kind of related because they're all on the list of high-pesticide foods.
So how can we have lists within a list? Well, that's,
what's called a nested list. Instead of our original dirty dozen,
we could create a new list called dirty_dozen,
and we set it equal to a list that contains two lists.
It contains fruits and it contains vegetables.
So now what effectively has happened is we've inserted this list inside here and
then we've inserted this list inside here.
So we now have a list that contains two lists.
And if I go ahead and print out this list, you'll be able to see its structure.
And it looks like this.
You'll notice that there's two brackets at the beginning and at the end
and the reason is because this is one list,
this is another list and this is also a list.
So this is yet another way of using lists and showing you the flexibility of
this particular data structure.
It's something that you're going to use a lot when you're writing Python code.
Now that we've talked more about index errors,
how to fix them and also nested lists,
you're ready to go to the next coding challenge.
So head over there and give that a go.