- For this self check exercise,
I'd like you to start out by creating a list
called rainbow containing green, orange and violet
and then perform the following four steps
and after each of these steps, display the current
set of values within the list so first,
we want you to figure out the index of violet
then use that to insert red before violet in the list.
Then, we want you to append yellow to the end of the list.
We want you to reverse the list elements
and finally, we want you to remove the element
that contains orange so go ahead and pause the video
and give those exercises a shot,
then you can come back here to see the answers.
Okay, so as you can see, I've already gone ahead
and created the list so let me execute that
to physically create it within this notebook.
By the way, that's a common error when you're executing
code in a notebook where you perhaps execute a cell
after a previous cell and the
previous cell has not been executed yet
so something may need to be defined that is not yet defined
and you could wind up with some errors if you're not careful
so we intend for our notebooks to be executed
from top to bottom in order but of course
when you're working in the context of a notebook,
you actually can execute cells in any order
by just clicking in a cell and telling it to execute.
So just be careful when you're working with our notebooks.
So, for our first part there in part A,
we want to insert the string red before the string violet
so the way we did that was to first use the index method
to find violet's index number within the rainbow list
and here we can see it's at index two so basically,
we're saying let's insert at index two the string red
and let's then go ahead and display the contents of rainbow
and indeed we did insert in front of violet.
Next, we wanted to append yellow to the end of the list
so we have the append method for that purpose
and of course we can evaluate rainbow once again
to confirm that yellow was appended.
To reverse the elements,
we can simply call the reverse method, so let's do that
and let's go ahead in the next cell here
and display the elements in reverse order.
And then finally, we wanted to remove the element
containing orange and we can do that with the remove method
and the last evaluation of the list shows you that indeed,
the orange object has been removed.
The orange string has been removed from the list.