1 00:00:00,000 --> 00:00:01,570 - [Instructor] In this video, 2 00:00:01,570 --> 00:00:03,760 we're going to introduce another way 3 00:00:03,760 --> 00:00:06,210 to delete elements from a list, 4 00:00:06,210 --> 00:00:09,740 using the del, or delete, statement, 5 00:00:09,740 --> 00:00:12,963 which is not only capable of deleting from a list, 6 00:00:12,963 --> 00:00:16,900 but also capable of deleting variables 7 00:00:16,900 --> 00:00:20,300 from an Interactive Python session as well. 8 00:00:20,300 --> 00:00:22,860 So we'll demonstrate those cases to you. 9 00:00:22,860 --> 00:00:24,880 Now, for the purpose of this example, 10 00:00:24,880 --> 00:00:27,486 we'll once again work with a list of numbers. 11 00:00:27,486 --> 00:00:31,006 For this one, let's say we want to produce the range 12 00:00:31,006 --> 00:00:34,800 of values zero through nine. 13 00:00:34,800 --> 00:00:37,890 And, of course, if we go ahead and evaluate numbers, 14 00:00:37,890 --> 00:00:41,020 we can see the contents of that list. 15 00:00:41,020 --> 00:00:45,119 Now, the del statement can be used to remove, 16 00:00:45,119 --> 00:00:49,839 from a list, any valid index position 17 00:00:49,839 --> 00:00:54,570 or any subset slice that you specify. 18 00:00:54,570 --> 00:00:56,998 So, for example, if I go ahead and say 19 00:00:56,998 --> 00:01:00,480 del numbers sub minus one, 20 00:01:00,480 --> 00:01:04,410 that says delete the last element of the list. 21 00:01:04,410 --> 00:01:07,650 And if I now go and reevaluate numbers, 22 00:01:07,650 --> 00:01:11,180 we can see there's one fewer element in the list. 23 00:01:11,180 --> 00:01:13,660 The nine has been removed. 24 00:01:13,660 --> 00:01:16,610 Now, like I said, you can also delete slices. 25 00:01:16,610 --> 00:01:19,910 So if I say delete numbers 26 00:01:19,910 --> 00:01:21,950 sub zero colon two, 27 00:01:21,950 --> 00:01:25,540 and again, zero in this case is implicit 28 00:01:25,540 --> 00:01:27,250 because it's the start of the list, 29 00:01:27,250 --> 00:01:30,330 so I could just say del numbers colon two 30 00:01:30,330 --> 00:01:31,856 in the square brackets, 31 00:01:31,856 --> 00:01:34,280 and in this case, 32 00:01:34,280 --> 00:01:36,420 we can go ahead and take a look at numbers 33 00:01:36,420 --> 00:01:38,820 and see that the first two elements, 34 00:01:38,820 --> 00:01:40,460 the slice from zero, 35 00:01:40,460 --> 00:01:43,632 up to but not including index position two, 36 00:01:43,632 --> 00:01:47,653 has been removed from the beginning of the list. 37 00:01:48,850 --> 00:01:50,780 Now, as you might expect, 38 00:01:50,780 --> 00:01:53,950 you don't have to delete consecutive elements 39 00:01:53,950 --> 00:01:57,740 in a slice because any valid slice can be used. 40 00:01:57,740 --> 00:01:59,591 So if I now go ahead and say 41 00:01:59,591 --> 00:02:04,243 let's delete from numbers every other remaining element, 42 00:02:04,243 --> 00:02:06,660 that's going to start from index zero, 43 00:02:06,660 --> 00:02:09,070 which will be inferred as the first index, 44 00:02:09,070 --> 00:02:10,730 go all the way through the list, 45 00:02:10,730 --> 00:02:12,680 so the length of the list will be inferred 46 00:02:12,680 --> 00:02:14,030 as the second index, 47 00:02:14,030 --> 00:02:16,460 and every other element will be removed. 48 00:02:16,460 --> 00:02:20,970 So we'll delete the two, four, six, and eight 49 00:02:20,970 --> 00:02:22,620 from the remaining list, 50 00:02:22,620 --> 00:02:26,320 and if we now go ahead and evaluate numbers, 51 00:02:26,320 --> 00:02:30,080 we see that only three, five, and seven are remaining. 52 00:02:30,080 --> 00:02:32,970 And, as you would also probably expect now 53 00:02:32,970 --> 00:02:35,140 based on what we've shown you with slices, 54 00:02:35,140 --> 00:02:37,606 we could even do delete numbers colon, 55 00:02:37,606 --> 00:02:41,294 in square brackets, which just says delete all the elements 56 00:02:41,294 --> 00:02:42,960 of the list numbers, 57 00:02:42,960 --> 00:02:45,940 and now it is an empty list. 58 00:02:45,940 --> 00:02:49,160 So, as I mentioned towards the beginning of this video, 59 00:02:49,160 --> 00:02:52,527 in addition to being able to delete elements from a list, 60 00:02:52,527 --> 00:02:54,760 which is a mutable object, 61 00:02:54,760 --> 00:02:57,830 so you can't do this for slices or strings for example, 62 00:02:57,830 --> 00:03:00,032 but in addition to modifying lists, 63 00:03:00,032 --> 00:03:02,630 you can also delete variables 64 00:03:02,630 --> 00:03:04,960 from the current session as well. 65 00:03:04,960 --> 00:03:07,360 So if I simply say delete numbers, 66 00:03:07,360 --> 00:03:10,480 that literally says remove the variable numbers 67 00:03:10,480 --> 00:03:13,150 from this interactive IPython session. 68 00:03:13,150 --> 00:03:15,720 So now if I try to evaluate numbers, 69 00:03:15,720 --> 00:03:18,311 it tells me that that name is not defined 70 00:03:18,311 --> 00:03:20,563 here in the current session.