1 00:00:08,680 --> 00:00:11,020 Welcome back. 2 00:00:11,020 --> 00:00:15,650 Earlier in this specialization, we took dive into aliasing, and the confusions 3 00:00:15,650 --> 00:00:19,940 that can occur when you mutate a list or a dictionary that has many aliases. 4 00:00:19,940 --> 00:00:23,420 Many different variable names pointing to the same list or dictionary. 5 00:00:24,580 --> 00:00:29,030 The same thing is true if you have instead of variable names 6 00:00:29,030 --> 00:00:30,710 pointing to a single list, 7 00:00:30,710 --> 00:00:35,110 if you have a single list that is included as an element in multiple other lists. 8 00:00:36,740 --> 00:00:40,590 If you mutate the inner list that is part of several outer lists, 9 00:00:40,590 --> 00:00:43,360 all those outer lists will look like the contents have changed. 10 00:00:44,860 --> 00:00:47,710 The best to keep track of these confusing situations will be 11 00:00:47,710 --> 00:00:49,650 with the reference diagram, of course. 12 00:00:52,610 --> 00:00:57,110 So let's took at example one way that you get a list included in multiple list 13 00:00:57,110 --> 00:01:02,000 by accident, and let's get this confusing situation is when you copy a list. 14 00:01:02,000 --> 00:01:05,897 As in this codes slip it, let's start by just running it. 15 00:01:09,249 --> 00:01:14,437 So you can see that on line1, we create a list called original, 16 00:01:14,437 --> 00:01:17,240 it's value is a nested list. 17 00:01:17,240 --> 00:01:22,786 The outer list has two inner lists as it's elements. 18 00:01:22,786 --> 00:01:25,980 Let's set ourselves up with a reference diagram. 19 00:01:25,980 --> 00:01:29,744 We're going to say that, Original, 20 00:01:34,643 --> 00:01:38,700 Is the variable name, and it's value is a list. 21 00:01:39,860 --> 00:01:42,063 That list has two items. 22 00:01:44,676 --> 00:01:51,885 The first item is itself a list, and that list has dogs, 23 00:01:54,036 --> 00:01:58,660 As an element, and it has puppies as an element. 24 00:02:07,481 --> 00:02:12,354 The second element of original is another list, 25 00:02:14,764 --> 00:02:21,707 Which is as it values cats and kittens. 26 00:02:29,704 --> 00:02:33,110 So that's how it happens on line 1. 27 00:02:33,110 --> 00:02:39,550 On line 2, we make a copy of original. 28 00:02:39,550 --> 00:02:44,730 We use the slice operator, colon inside of square brackets. 29 00:02:44,730 --> 00:02:47,970 You may recall that always creates a slice. 30 00:02:47,970 --> 00:02:53,734 If I said, original[1:4], 31 00:02:53,734 --> 00:02:58,950 that would Make a slice beginning 32 00:02:58,950 --> 00:03:03,850 with the item at index one and going up to but not including position four. 33 00:03:03,850 --> 00:03:07,650 Of course, I can't do that for this list because it only has two items. 34 00:03:08,800 --> 00:03:14,310 But if we leave off the value before the colon, 35 00:03:14,310 --> 00:03:16,260 we start at the beginning of the list. 36 00:03:16,260 --> 00:03:21,170 If we leave off the value after the colon we go all the way to the end of the list. 37 00:03:21,170 --> 00:03:24,710 So this says take a slice beginning at the beginning of original, and 38 00:03:24,710 --> 00:03:25,790 going to end of original. 39 00:03:26,950 --> 00:03:32,874 So that makes a new list, and that new list has has its elements, 40 00:03:32,874 --> 00:03:36,402 the same elements that original had. 41 00:03:38,437 --> 00:03:42,830 The first element is dogs and puppies, the second element is cats and kittens. 42 00:03:42,830 --> 00:03:47,481 And we assign that to a variable called copied_version. 43 00:03:54,817 --> 00:03:56,565 That's what happens on line 2. 44 00:03:58,030 --> 00:04:00,500 And just to show you what the output looks like. 45 00:04:02,068 --> 00:04:06,730 On line 3, we print out copied _version and sure enough, 46 00:04:06,730 --> 00:04:10,280 it looks like it has exactly the same stuff that original has in it. 47 00:04:12,630 --> 00:04:19,053 On line 4, we checked is copied_version the same object as original. 48 00:04:19,053 --> 00:04:21,215 And the answer to that is false. 49 00:04:21,215 --> 00:04:23,650 Copied_version is pointing to one object. 50 00:04:24,950 --> 00:04:26,910 Original is pointing to a different object. 51 00:04:28,490 --> 00:04:34,360 One line 5, we check whether they have the same contents. 52 00:04:34,360 --> 00:04:36,150 The answer to that true. 53 00:04:36,150 --> 00:04:39,368 So original and copied are not the same object, 54 00:04:39,368 --> 00:04:43,154 they're different objects, but they're both list. 55 00:04:43,154 --> 00:04:47,458 They both have two elements, and those elements are these inner list, 56 00:04:47,458 --> 00:04:50,050 dog, puppies, and cats, kittens. 57 00:04:50,050 --> 00:04:52,890 It's true that they are equal, equal. 58 00:04:52,890 --> 00:04:56,622 It's not true that they are is each other. 59 00:04:56,622 --> 00:04:59,500 So copied_version is not original but 60 00:04:59,500 --> 00:05:02,820 it is equal to the original, it has the same contents. 61 00:05:04,668 --> 00:05:08,210 On line 6, we see where our confusions are going to happen 62 00:05:08,210 --> 00:05:12,050 when we have multiple outer list that point to the same inner list. 63 00:05:13,190 --> 00:05:21,910 So original square bracket 0 is the first inner list, that's dog's puppies. 64 00:05:21,910 --> 00:05:26,110 And on line 6, we say, .append of canines. 65 00:05:26,110 --> 00:05:33,350 So that is going to append to the end of this list, another item. 66 00:05:33,350 --> 00:05:39,390 But notice that this item is canines in square brackets. 67 00:05:39,390 --> 00:05:41,440 So this item is actually another list. 68 00:05:43,820 --> 00:05:49,536 That list has only one item, canines. 69 00:05:53,388 --> 00:05:57,623 But it is a list, it's not just this string canines. 70 00:05:57,623 --> 00:06:06,670 So now on line 7, if we print original, We get this list here. 71 00:06:06,670 --> 00:06:11,050 Where the first element is dogs, puppies like before, but 72 00:06:11,050 --> 00:06:15,450 we've appended to the end of that list, the list canines. 73 00:06:16,970 --> 00:06:22,615 And then the second element is still cats and kittens. 74 00:06:25,542 --> 00:06:28,740 On line 9, we print the copied version. 75 00:06:29,990 --> 00:06:33,590 Now on line 6, we just said to change original, 76 00:06:33,590 --> 00:06:36,740 we didn't say anything about changing copied_version. 77 00:06:36,740 --> 00:06:38,640 But on line 9, you can see 78 00:06:39,950 --> 00:06:45,500 that this square bracket canines has magically appeared after dogs and puppies. 79 00:06:45,500 --> 00:06:50,465 And that's because copied_version, even though we didn't say to change it, 80 00:06:50,465 --> 00:06:55,365 copied_version has as its first element this list, and we mutated that list. 81 00:06:59,329 --> 00:07:01,000 That's shallow copies for you. 82 00:07:01,000 --> 00:07:02,400 See you next time.