Welcome back. There are two ways to add
stuff to the end of a list one we've seen already,
and illustrated here is to append.
So, origlist on line one,
gets assigned to a list of three things,
and then on line three that list gets an additional item at the end,
it gets "cat" added to the end.
The second way, is using concatenation, the plus operator.
So, on line one,
we create the same list,
but on line three we are now going to use concatenation, and then reassignment.
So, we concatenate the original list with a new list,
and the new list has only one item in it,
and then we reassign that back to the same variable.
So, origlist is going to end up being bound to a new list,
that has four items 45,
32, 88, and the word "cat".
Now, in codelens, it's not actually going to show us that we have a new list here,
because there's nothing pointing anymore to the old list,
and so it doesn't bother to show it to us anymore.
So, it'll look like we've just replaced the old list
but it's actually a new list with four items.
So, sure enough that's what we get.
Now let's look at a little more complicated one.
Here on line one again,
we're creating origlist with the three values in it,
and then we're just printing out some things to remind us of what those are,
but then on line four we're doing something different.
Instead of reassigning the concatenated list of origlist plus "cat",
back to origlist, we're reassigning it to a different variable called newlist.
So, let's see how this is going to play out in codelens.
On line one, we assigned the list to origlist,
and we're just printing out some stuff.
Line four, we're making a new variable,
and it's pointing to the newlist.
So, remember before I didn't show us that the old list was still unchanged,
here we're seeing that the old list with
just three items in it is still bound to the variable origlist.
Even though we did a concatenation operator,
that did not change the contents of the actual list.
It made a newlist,
and then we bound that to a new variable.
So, then we print some stuff out,
and now we actually are using an append operation.
That's going to stick a new value,
and additional value onto origlist, and so,
we're going to get something added on here to origlist,
you're going to get "cat" here as well.
That's what we'll get. Let's run it and see.
Sure enough "cat" is added on to that original list as well and then,
we print some more things.
Now we've printed out the identifiers,
like these long numbers Just so you can see that
origlist is pointing to one object with one identifier,
and newlist is pointing to a different object with a different identifier.
There's one more tricky thing to know about append and concatenate,
and that is that, plus equals sign that we've seen previously.
We've seen it as we had things like X,
plus equals 1, and we described that as being a synonym,
just another way of saying X equals X plus 1.
In the world of mutable lists,
that's not quite true anymore.
They're not quite the same.
Now, what's going to happen with the plus equal sign when we do it with a list,
is that actually is going to mutate the list,
it doesn't make a newlist,
and concatenate to that.
The only time that's going to matter is if we have some other alias for the same list.
So, in this code, let's run it.
The first line of code just assigns a list of three items to origlist.
The second line of code makes a new variable that points to that same object,
so aliaslist is an alias for origlist.
Now we're on to line three.
On line three we have this origlist, plus equals "cat".
We're going see that that's a slightly different thing,
than saying origlist equals origlist, plus the new thing.
The difference is that we're actually going to extend the original object,
and because we extend the original object rather than making a copy and a new one,
we will also affect any aliases for it.
So, when we run this,
we will find that not only does origlist now have "cat" as its last item,
but so does aliaslist.
So, this is a different outcome than we would have
gotten had we run this line of code instead of line three.
My suggestion is, with lists don't use the plus equal operator it's just too confusing.
Stick with what we have on line four,
where we say origlist equals origlist, plus another list.
Don't use the plus equal operator with lists.
On the textbook page about append versus concatenate as with many of the other pages,
there are lots of useful exercises,
and I encourage you to try those out,
and we'll see you next time.