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 our states_of_america,
and you might remember that there are 50 states in America,
but if you don't, because you're 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 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?
What do we get instead?
Well, we get an error.
It's called an IndexError.
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, say
number_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?
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 -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 crunched through a whole lot of data, probably using Python, and
they released their Dirty Dozen, a list of the fruits and vegetables that have the most pesticides.
And it's 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 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?
They're 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 just 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.