1 00:00:07,910 --> 00:00:12,120 Welcome back. There are two ways to add 2 00:00:12,120 --> 00:00:15,690 stuff to the end of a list one we've seen already, 3 00:00:15,690 --> 00:00:19,755 and illustrated here is to append. 4 00:00:19,755 --> 00:00:22,980 So, origlist on line one, 5 00:00:22,980 --> 00:00:26,010 gets assigned to a list of three things, 6 00:00:26,010 --> 00:00:29,670 and then on line three that list gets an additional item at the end, 7 00:00:29,670 --> 00:00:32,460 it gets "cat" added to the end. 8 00:00:32,460 --> 00:00:41,325 The second way, is using concatenation, the plus operator. 9 00:00:41,325 --> 00:00:42,825 So, on line one, 10 00:00:42,825 --> 00:00:44,665 we create the same list, 11 00:00:44,665 --> 00:00:53,590 but on line three we are now going to use concatenation, and then reassignment. 12 00:00:55,640 --> 00:01:01,610 So, we concatenate the original list with a new list, 13 00:01:01,610 --> 00:01:03,815 and the new list has only one item in it, 14 00:01:03,815 --> 00:01:08,135 and then we reassign that back to the same variable. 15 00:01:08,135 --> 00:01:13,220 So, origlist is going to end up being bound to a new list, 16 00:01:13,220 --> 00:01:17,085 that has four items 45, 17 00:01:17,085 --> 00:01:27,660 32, 88, and the word "cat". 18 00:01:37,070 --> 00:01:43,430 Now, in codelens, it's not actually going to show us that we have a new list here, 19 00:01:43,430 --> 00:01:48,579 because there's nothing pointing anymore to the old list, 20 00:01:48,579 --> 00:01:51,000 and so it doesn't bother to show it to us anymore. 21 00:01:51,000 --> 00:01:53,470 So, it'll look like we've just replaced the old list 22 00:01:53,470 --> 00:01:57,050 but it's actually a new list with four items. 23 00:02:01,040 --> 00:02:03,460 So, sure enough that's what we get. 24 00:02:03,460 --> 00:02:06,530 Now let's look at a little more complicated one. 25 00:02:09,680 --> 00:02:12,755 Here on line one again, 26 00:02:12,755 --> 00:02:15,580 we're creating origlist with the three values in it, 27 00:02:15,580 --> 00:02:20,320 and then we're just printing out some things to remind us of what those are, 28 00:02:20,320 --> 00:02:22,675 but then on line four we're doing something different. 29 00:02:22,675 --> 00:02:28,815 Instead of reassigning the concatenated list of origlist plus "cat", 30 00:02:28,815 --> 00:02:35,045 back to origlist, we're reassigning it to a different variable called newlist. 31 00:02:35,045 --> 00:02:38,710 So, let's see how this is going to play out in codelens. 32 00:02:41,660 --> 00:02:48,615 On line one, we assigned the list to origlist, 33 00:02:48,615 --> 00:02:51,495 and we're just printing out some stuff. 34 00:02:51,495 --> 00:02:54,869 Line four, we're making a new variable, 35 00:02:54,869 --> 00:02:56,610 and it's pointing to the newlist. 36 00:02:56,610 --> 00:03:01,115 So, remember before I didn't show us that the old list was still unchanged, 37 00:03:01,115 --> 00:03:04,430 here we're seeing that the old list with 38 00:03:04,430 --> 00:03:09,260 just three items in it is still bound to the variable origlist. 39 00:03:09,260 --> 00:03:12,650 Even though we did a concatenation operator, 40 00:03:12,650 --> 00:03:17,150 that did not change the contents of the actual list. 41 00:03:17,150 --> 00:03:19,330 It made a newlist, 42 00:03:19,330 --> 00:03:23,620 and then we bound that to a new variable. 43 00:03:27,440 --> 00:03:30,970 So, then we print some stuff out, 44 00:03:32,870 --> 00:03:37,175 and now we actually are using an append operation. 45 00:03:37,175 --> 00:03:39,950 That's going to stick a new value, 46 00:03:39,950 --> 00:03:44,635 and additional value onto origlist, and so, 47 00:03:44,635 --> 00:03:49,895 we're going to get something added on here to origlist, 48 00:03:49,895 --> 00:03:53,590 you're going to get "cat" here as well. 49 00:03:57,020 --> 00:04:01,960 That's what we'll get. Let's run it and see. 50 00:04:02,750 --> 00:04:08,610 Sure enough "cat" is added on to that original list as well and then, 51 00:04:08,610 --> 00:04:10,230 we print some more things. 52 00:04:10,230 --> 00:04:12,975 Now we've printed out the identifiers, 53 00:04:12,975 --> 00:04:16,790 like these long numbers Just so you can see that 54 00:04:16,790 --> 00:04:21,155 origlist is pointing to one object with one identifier, 55 00:04:21,155 --> 00:04:27,420 and newlist is pointing to a different object with a different identifier. 56 00:04:27,430 --> 00:04:33,150 There's one more tricky thing to know about append and concatenate, 57 00:04:33,150 --> 00:04:38,400 and that is that, plus equals sign that we've seen previously. 58 00:04:38,810 --> 00:04:43,385 We've seen it as we had things like X, 59 00:04:43,385 --> 00:04:51,065 plus equals 1, and we described that as being a synonym, 60 00:04:51,065 --> 00:04:55,200 just another way of saying X equals X plus 1. 61 00:04:56,630 --> 00:04:59,355 In the world of mutable lists, 62 00:04:59,355 --> 00:05:01,890 that's not quite true anymore. 63 00:05:01,890 --> 00:05:05,295 They're not quite the same. 64 00:05:05,295 --> 00:05:11,375 Now, what's going to happen with the plus equal sign when we do it with a list, 65 00:05:11,375 --> 00:05:13,625 is that actually is going to mutate the list, 66 00:05:13,625 --> 00:05:16,295 it doesn't make a newlist, 67 00:05:16,295 --> 00:05:18,350 and concatenate to that. 68 00:05:18,350 --> 00:05:23,435 The only time that's going to matter is if we have some other alias for the same list. 69 00:05:23,435 --> 00:05:28,110 So, in this code, let's run it. 70 00:05:28,610 --> 00:05:34,295 The first line of code just assigns a list of three items to origlist. 71 00:05:34,295 --> 00:05:39,380 The second line of code makes a new variable that points to that same object, 72 00:05:39,380 --> 00:05:42,835 so aliaslist is an alias for origlist. 73 00:05:42,835 --> 00:05:44,985 Now we're on to line three. 74 00:05:44,985 --> 00:05:49,365 On line three we have this origlist, plus equals "cat". 75 00:05:49,365 --> 00:05:53,855 We're going see that that's a slightly different thing, 76 00:05:53,855 --> 00:06:10,470 than saying origlist equals origlist, plus the new thing. 77 00:06:23,910 --> 00:06:31,295 The difference is that we're actually going to extend the original object, 78 00:06:31,295 --> 00:06:35,530 and because we extend the original object rather than making a copy and a new one, 79 00:06:35,530 --> 00:06:38,815 we will also affect any aliases for it. 80 00:06:38,815 --> 00:06:41,840 So, when we run this, 81 00:06:47,480 --> 00:06:54,810 we will find that not only does origlist now have "cat" as its last item, 82 00:06:54,810 --> 00:06:57,035 but so does aliaslist. 83 00:06:57,035 --> 00:06:59,650 So, this is a different outcome than we would have 84 00:06:59,650 --> 00:07:04,750 gotten had we run this line of code instead of line three. 85 00:07:04,750 --> 00:07:12,395 My suggestion is, with lists don't use the plus equal operator it's just too confusing. 86 00:07:12,395 --> 00:07:15,305 Stick with what we have on line four, 87 00:07:15,305 --> 00:07:20,030 where we say origlist equals origlist, plus another list. 88 00:07:20,030 --> 00:07:24,910 Don't use the plus equal operator with lists. 89 00:07:25,400 --> 00:07:31,370 On the textbook page about append versus concatenate as with many of the other pages, 90 00:07:31,370 --> 00:07:33,440 there are lots of useful exercises, 91 00:07:33,440 --> 00:07:36,530 and I encourage you to try those out, 92 00:07:36,530 --> 00:07:38,880 and we'll see you next time.