Let's see some useful methods that operate on lists.
Some of these methods will mutate the list,
some of them will just return a value.
So, we've got a lot of things in this code.
Let's run it using CodeLens so that we can step through it one step at a time.
Our first command, just creates an empty list.
The next thing calls the append method on it, that's line two.
So, just as with elixir.forward we have mylist.append,
that means, run the append method on mylist,
which says, add a new item to the end.
In this case, the item will be
wherever we pass in as an argument it's going to be the value five.
So, we start with an empty list.
After we run this, we'll have a list containing one element, the value five.
If I do it again, on line three,
now the value 27 is going to be appended to the end of the list.
So, we'll have five and 27.
I can do it a third time,
this time three, will be appended to the end of the list.
It's helpful to notice here that the variable mylist is always bound to that list object.
So, we have the arrow pointing to it.
That never changes throughout all of this,
what changes is the list itself,
it's getting additional elements added to it.
So, we get a fourth element there.
If I now print it out,
it'll show up in the output window.
A second useful method is insert.
Append always sticks an item at the end,
insert, inserts the item wherever you tell it to.
In this case, we're going to tell it to insert at position number one.
So, it's going to go to position number one and it's going to insert it.
It will not erase the current contents at position number one,
that's what we had with assignment.
Recall, we could do something like
mylist square bracket one equals a new value.
That would replace the 27 with 12,
but that's now what happens with our insert command.
Our insert command splices our new value in right before position
one and we'll go
forward and sure enough,
the 12 has been inserted there between the five and the 27.
Print that out and now the list has five items in it.
Line 10 introduces a new method called count.
The argument that you pass to count in this case,12,
is not a position in the list, it's a value.
The count operation is going go through all of the values in
the list and count how many of them are equal to 12.
So, in this case, the count will be two.
That is something you did with an accumulation pattern in our previous lesson,
but the count method just does it for you,
counts how many times 12 appears in mylist.
Index is a method,
where we will pass
in a value and it will tell us the position where that value can be found.
So, we'll pass in value three and it'll tell us the index,
the position where that three can be found,
perhaps, somewhat confusingly, it's also three here.
So, we're going to get back the value three.
Print mylist.count to five,
is going to look through and find all the places where
five appears, there's only one of them.
So, we'll get a count of one.
Mylist.reverse, takes the order of the items in the list and changes them.
So, the last becomes first and then we get the three,
the 27, the 12 and the five.
These values are actually going to go back into the original positions.
So, the list itself is being changed.
We're not making a new copy of the list.
So, when I run that, it actually reverses the items,
I printed it that come out in the opposite order that they were in before.
Sort is another method,
it's going to take the items in the list and just reorder them from smallest to biggest.
So, we'll get three,
then five,then 12 and another 12 and 27.
So, sort and reverse,
keep all the same items,
but they rearrange them into a different order.
Our next method, we see on line 21 is.remove.
You've seen previously a deletion operation del,
which takes a position
and whatever item is at position one,
would get deleted if I write del and mylist square bracket one.
That remove, doesn't specify a position,
it specifies a value.
So, it's going to remove the five wherever it is.
Now, the five is gone.
There's one more operation for removing items,
pop, it removes the last item from the list.
So, in this case, when we pop mylist,
we will get 27 and it actually returns that value.
So, the list will be changed and we'll get the value 27 back.
In this case, we've chosen to assign the results of
the pop operation to a new variable called lastitem.
Sure enough, lastitem now has the value 27.
So, to summarize, you've learned here how to use several mutating operations,
mutating methods on list, append,
index, reverse, sort, remove and pop.
But also, seen a couple of non-mutating methods,
index and count that return values and pop,
that was a mutating method that changed the list and also returned a value.
Of all of these, I would say append is the method that's the most important.
You'll be using append all of the time. See you next time.