1 00:00:00,690 --> 00:00:02,560 - [Instructor] In this video we'll continue 2 00:00:02,560 --> 00:00:05,670 the iPython session from the preceding video 3 00:00:05,670 --> 00:00:09,280 and we'll keep working with our list called numbers 4 00:00:09,280 --> 00:00:12,780 this time to modify the contents of the list 5 00:00:12,780 --> 00:00:15,070 using slicing operations. 6 00:00:15,070 --> 00:00:17,950 So let me paste in our initial slice operation 7 00:00:17,950 --> 00:00:19,540 that we're going to discuss here. 8 00:00:19,540 --> 00:00:23,140 We have the list numbers, and we're saying 9 00:00:23,140 --> 00:00:26,200 select the subset of that list with the slice 10 00:00:26,200 --> 00:00:28,320 of that list from index zero, 11 00:00:28,320 --> 00:00:31,090 up to but not including index three. 12 00:00:31,090 --> 00:00:33,920 Now just at the top here, here's the original list 13 00:00:33,920 --> 00:00:34,780 that we're talking about. 14 00:00:34,780 --> 00:00:38,250 So from index zero we're going to go zero, one, two, 15 00:00:38,250 --> 00:00:41,290 up to but not including index position three. 16 00:00:41,290 --> 00:00:44,240 So we're talking about the slice containing two, 17 00:00:44,240 --> 00:00:48,610 three, and five, and we're going to assign to that slice 18 00:00:48,610 --> 00:00:50,910 this list with the same number of elements. 19 00:00:50,910 --> 00:00:54,950 In this case two, three, and five as the words 20 00:00:54,950 --> 00:00:57,740 that we would like to replace the numeric values. 21 00:00:57,740 --> 00:01:00,590 And by the way, when you're assigning to a slice, 22 00:01:00,590 --> 00:01:03,410 you do not need to 23 00:01:03,410 --> 00:01:05,230 have the same number of elements 24 00:01:05,230 --> 00:01:08,310 in the new slice as in the original slice, 25 00:01:08,310 --> 00:01:11,400 so you can actually change the size of a slice 26 00:01:11,400 --> 00:01:15,030 by assigning a sequence of values to that slice, 27 00:01:15,030 --> 00:01:18,000 and we'll talk about that a couple of times in this video. 28 00:01:18,000 --> 00:01:21,480 So let's execute that and of course to check what's 29 00:01:21,480 --> 00:01:25,330 now in the list numbers we can evaluate it, 30 00:01:25,330 --> 00:01:27,950 and you can see that we've replaced the first 31 00:01:27,950 --> 00:01:32,250 three elements with the word equivalents of those values. 32 00:01:32,250 --> 00:01:37,250 Now in addition to simply replacing values within a slice 33 00:01:37,640 --> 00:01:42,470 we can delete elements using slice notation as well. 34 00:01:42,470 --> 00:01:47,030 So, for example, if I say number sub-zero colon three again 35 00:01:47,030 --> 00:01:51,310 we're still selecting the first three elements of that list. 36 00:01:52,660 --> 00:01:55,260 And if we assign to that an empty list, 37 00:01:55,260 --> 00:01:57,840 that literally says delete that slice 38 00:01:57,840 --> 00:01:59,670 from the original list. 39 00:01:59,670 --> 00:02:02,960 So in this case to prove that we can go ahead 40 00:02:02,960 --> 00:02:05,850 and say numbers, and now you see the first 41 00:02:05,850 --> 00:02:10,500 three elements have been physically removed from the list. 42 00:02:10,500 --> 00:02:11,860 Now 43 00:02:11,860 --> 00:02:13,700 so far, we've demonstrated 44 00:02:13,700 --> 00:02:16,370 a couple of modification operations. 45 00:02:16,370 --> 00:02:20,760 Let's bring back the original list for the next operation 46 00:02:20,760 --> 00:02:23,930 that we're going to present to you. 47 00:02:23,930 --> 00:02:26,940 And by the way, here's a handy little trick 48 00:02:26,940 --> 00:02:29,910 that you can use to recall a prior snippet. 49 00:02:29,910 --> 00:02:32,820 So I know at some point previously, 50 00:02:32,820 --> 00:02:36,750 I assigned something to the variable numbers. 51 00:02:36,750 --> 00:02:39,120 If I type in the beginning of that assignment 52 00:02:39,120 --> 00:02:43,130 and hit the up arrow key it will recall the last statement 53 00:02:43,130 --> 00:02:46,420 that started with what I had typed so far. 54 00:02:46,420 --> 00:02:50,660 So I'm now going to reset the variable numbers 55 00:02:50,660 --> 00:02:53,280 to a list containing the same values 56 00:02:53,280 --> 00:02:56,420 that we started with in the preceding video. 57 00:02:56,420 --> 00:02:57,500 Now, 58 00:02:57,500 --> 00:02:58,900 we can 59 00:02:58,900 --> 00:03:00,220 assign 60 00:03:00,220 --> 00:03:02,210 slices to the list 61 00:03:02,210 --> 00:03:05,530 in non-consecutive elements, 62 00:03:05,530 --> 00:03:07,800 and so that's what I want to demonstrate next here. 63 00:03:07,800 --> 00:03:10,850 So if I say numbers, colon, colon, two, 64 00:03:10,850 --> 00:03:13,420 that says for every other element 65 00:03:13,420 --> 00:03:16,940 starting from the first element of the list numbers, 66 00:03:16,940 --> 00:03:19,740 we would like to select that slice. 67 00:03:19,740 --> 00:03:23,890 And let's say we wanted to assign the value 100 68 00:03:23,890 --> 00:03:25,630 to every other element. 69 00:03:25,630 --> 00:03:29,610 So we have eight elements in this case, we want to 70 00:03:31,040 --> 00:03:33,880 place 100 into element zero, 71 00:03:33,880 --> 00:03:36,200 two, four, and six. 72 00:03:36,200 --> 00:03:38,430 So as a result of this it's going to take 73 00:03:38,430 --> 00:03:41,440 the first value in the right-hand side and 74 00:03:41,440 --> 00:03:44,940 place it into the first element of the resulting slice, 75 00:03:44,940 --> 00:03:47,350 the second value on the right-hand side will be placed 76 00:03:47,350 --> 00:03:49,800 into the second element of the resulting slice, 77 00:03:49,800 --> 00:03:52,520 and we'll do the same thing for the third 78 00:03:52,520 --> 00:03:55,340 and fourth elements of the resulting slice as well. 79 00:03:55,340 --> 00:03:57,900 And of course if we now say numbers, 80 00:03:57,900 --> 00:04:00,900 you can see the four 100 values placed 81 00:04:00,900 --> 00:04:05,090 into the corresponding locations within that list. 82 00:04:05,090 --> 00:04:07,720 Now for the next part of this example, 83 00:04:07,720 --> 00:04:11,470 the first thing I want to do is just take the ID of numbers, 84 00:04:11,470 --> 00:04:15,290 because I want to show you when new objects are being 85 00:04:15,290 --> 00:04:19,530 created versus modifying the existing object. 86 00:04:19,530 --> 00:04:22,610 So we'll simply leave this here for the next 87 00:04:22,610 --> 00:04:23,853 part of the discussion. 88 00:04:25,010 --> 00:04:27,020 So we showed you up above that you can 89 00:04:27,020 --> 00:04:30,110 assign an empty list to a slice 90 00:04:30,110 --> 00:04:32,901 to delete elements from that list. 91 00:04:32,901 --> 00:04:36,660 Well, it turns out that you can use that technique 92 00:04:36,660 --> 00:04:39,870 to delete all of the elements of the list as well. 93 00:04:39,870 --> 00:04:44,600 So for example, if I say numbers, sub-colon, 94 00:04:44,600 --> 00:04:45,490 like that, 95 00:04:45,490 --> 00:04:48,740 it's going to implicitly insert the zero for 96 00:04:48,740 --> 00:04:51,040 the starting index, and the length of numbers for 97 00:04:51,040 --> 00:04:52,450 the ending index. 98 00:04:52,450 --> 00:04:55,320 And I can then assign to that an empty list 99 00:04:55,320 --> 00:04:58,590 which literally deletes all the elements 100 00:04:58,590 --> 00:05:00,320 from the original list. 101 00:05:00,320 --> 00:05:02,690 So if I go ahead and execute that operation, 102 00:05:02,690 --> 00:05:05,720 I can evaluate numbers to see that indeed it's empty. 103 00:05:05,720 --> 00:05:08,720 And I just want to prove to you that it's the same 104 00:05:08,720 --> 00:05:10,300 list object still. 105 00:05:10,300 --> 00:05:13,520 So let me recall my ID expression here, 106 00:05:13,520 --> 00:05:15,670 and when I execute that you can see 107 00:05:15,670 --> 00:05:19,210 that the ID's of these two objects are identical, 108 00:05:19,210 --> 00:05:21,400 so it's still the same numbers list, 109 00:05:21,400 --> 00:05:25,100 we've simply deleted all the elements by using 110 00:05:25,100 --> 00:05:28,880 a slice operation on the left side of the assignment. 111 00:05:28,880 --> 00:05:32,750 Now that's very different from doing this assignment 112 00:05:32,750 --> 00:05:35,350 which simply aims the variable numbers 113 00:05:35,350 --> 00:05:39,490 at a completely new list object that contains no elements. 114 00:05:39,490 --> 00:05:41,630 So let's go ahead and execute that, 115 00:05:41,630 --> 00:05:43,750 and of course if we evaluate numbers 116 00:05:43,750 --> 00:05:45,450 we still see that it's empty, 117 00:05:45,450 --> 00:05:48,920 but if we recall that ID expression once again, 118 00:05:48,920 --> 00:05:51,660 you can see this time that indeed 119 00:05:51,660 --> 00:05:54,320 we have a new object identity, 120 00:05:54,320 --> 00:05:57,290 so this was a physically new object, 121 00:05:57,290 --> 00:06:00,563 as opposed to modifying the existing one.