WEBVTT 1 00:00:00.494 --> 00:00:02.490 line:15% Hi, guys,nd welcome back. 2 00:00:02.490 --> 00:00:05.680 line:15% In this video, I wanted to tell you about slicing. 3 00:00:05.680 --> 00:00:07.480 Slicing is the process of getting 4 00:00:07.480 --> 00:00:10.850 a part of a list or other iterable. 5 00:00:10.850 --> 00:00:13.120 Imagine you've got a list of your friends, 6 00:00:13.120 --> 00:00:14.710 and you wanted your friends 7 00:00:14.710 --> 00:00:17.660 between two and four. 8 00:00:17.660 --> 00:00:20.070 You can do print, friends, 9 00:00:20.070 --> 00:00:21.890 and then in square brackets, 10 00:00:21.890 --> 00:00:23.863 2 colon 4. 11 00:00:24.810 --> 00:00:26.230 And what this means is 12 00:00:26.230 --> 00:00:28.910 you're going to get Anna and Bob. 13 00:00:28.910 --> 00:00:32.730 That's two, three, and stops at 14 00:00:32.730 --> 00:00:35.560 four so it doesn't get you four. 15 00:00:35.560 --> 00:00:38.540 This here is list slicing 16 00:00:38.540 --> 00:00:40.330 and you can do this with strings as well 17 00:00:40.330 --> 00:00:41.890 if you want or with tuples 18 00:00:41.890 --> 00:00:42.940 and what it allows you to do 19 00:00:42.940 --> 00:00:45.572 is to get a segment of a list 20 00:00:45.572 --> 00:00:47.740 or a tuple or a string. 21 00:00:47.740 --> 00:00:50.410 It can be really useful at times, 22 00:00:50.410 --> 00:00:52.430 but it is particularly useful 23 00:00:52.430 --> 00:00:54.290 because you can actually skip 24 00:00:54.290 --> 00:00:55.910 some of these numbers. 25 00:00:55.910 --> 00:00:58.970 For example, if you want to only get 26 00:00:58.970 --> 00:01:00.450 elements in the list, 27 00:01:00.450 --> 00:01:02.170 except the first one, 28 00:01:02.170 --> 00:01:05.200 you can do one colon nothing. 29 00:01:05.200 --> 00:01:08.250 And what that means is one to the end 30 00:01:08.250 --> 00:01:09.290 of the list. 31 00:01:09.290 --> 00:01:10.950 So if you do this, you're going to get 32 00:01:10.950 --> 00:01:14.090 all your friends, except the first one. 33 00:01:14.090 --> 00:01:16.990 Similarly, if you put four in here, 34 00:01:16.990 --> 00:01:19.570 it will start at the very start of the list, 35 00:01:19.570 --> 00:01:21.973 colon, and at four. 36 00:01:23.610 --> 00:01:25.160 So, we will get Rolf, Charlie, 37 00:01:25.160 --> 00:01:26.930 Anna, and Bob, and we will skip 38 00:01:26.930 --> 00:01:29.510 the fourth one and any later ones as well. 39 00:01:29.510 --> 00:01:31.640 Now, if you remove both numbers 40 00:01:31.640 --> 00:01:34.340 so you just do colon inside square brackets, 41 00:01:34.340 --> 00:01:36.370 you'll actually get all the elements back, 42 00:01:36.370 --> 00:01:38.470 but with a key difference. 43 00:01:38.470 --> 00:01:41.200 You're not getting the same list. 44 00:01:41.200 --> 00:01:43.150 It is a new list that you're building. 45 00:01:43.150 --> 00:01:45.050 And at this point, that doesn't really matter. 46 00:01:45.050 --> 00:01:46.970 It doesn't have a lot of meaning to you, 47 00:01:46.970 --> 00:01:49.270 but it just gives you a new list, and this is 48 00:01:49.270 --> 00:01:50.965 the way to do that in Python. If you want 49 00:01:50.965 --> 00:01:53.890 a new list which contains all of the elements 50 00:01:53.890 --> 00:01:56.793 of the previous list, you can do this. 51 00:01:57.730 --> 00:01:59.980 If you just do this, you're just getting 52 00:01:59.980 --> 00:02:02.720 the same list. So exactly the same thing. 53 00:02:02.720 --> 00:02:05.030 Now with list slicing you can actually use 54 00:02:05.030 --> 00:02:07.510 negative numbers. For example, 55 00:02:07.510 --> 00:02:11.650 minus three up to the end of the list. 56 00:02:11.650 --> 00:02:13.200 And what this does is it starts counting 57 00:02:13.200 --> 00:02:17.780 from the end. So, you will get back, Anna, 58 00:02:17.780 --> 00:02:20.530 which is, the element starting from the end, 59 00:02:20.530 --> 00:02:23.240 the third one. Bob and Jen. 60 00:02:23.240 --> 00:02:27.250 Because again, minus three is the starting point, 61 00:02:27.250 --> 00:02:29.360 and when you skip the ending point, 62 00:02:29.360 --> 00:02:31.950 that is the end of the list. 63 00:02:31.950 --> 00:02:35.410 So you could do minus three to four, for example, 64 00:02:35.410 --> 00:02:39.360 and you get Anna and Bob because it stops at four. 65 00:02:39.360 --> 00:02:42.030 Or you can do minus three colon empty, 66 00:02:42.030 --> 00:02:44.890 and then it'll run to the end of the list. 67 00:02:44.890 --> 00:02:49.740 You can do this here so it will go from 68 00:02:49.740 --> 00:02:51.190 the start of the list 69 00:02:51.190 --> 00:02:53.280 because you haven't put anything in there 70 00:02:53.280 --> 00:02:57.660 up to the second to last element: Bob. 71 00:02:57.660 --> 00:03:00.910 So you get Rolf, Charlie, and Anna. 72 00:03:00.910 --> 00:03:03.210 This is useful if you wanted all your friends 73 00:03:03.210 --> 00:03:05.720 except the last two, for example. 74 00:03:05.720 --> 00:03:08.350 Finally, you can also do minus three, 75 00:03:08.350 --> 00:03:12.350 and for example minus one. And again, 76 00:03:12.350 --> 00:03:16.220 that gives you from the third element from the end Anna, 77 00:03:16.220 --> 00:03:19.360 up to the first element from the end, Jen. 78 00:03:19.360 --> 00:03:22.830 But, it does not include this one as always, 79 00:03:22.830 --> 00:03:25.450 so you just get Anna and Bob. 80 00:03:25.450 --> 00:03:27.980 There's a lot more stuff you can do with slices, 81 00:03:27.980 --> 00:03:30.600 but I just wanted to give you a very quick primer. 82 00:03:30.600 --> 00:03:33.440 The first number is the starting point, 83 00:03:33.440 --> 00:03:36.580 and the second number is the ending point. 84 00:03:36.580 --> 00:03:38.760 We're gonna link, in the researching section 85 00:03:38.760 --> 00:03:41.410 of this lecture, a few blog posts that we've written 86 00:03:41.410 --> 00:03:45.130 on list slicing. Some of them may be a bit advanced, 87 00:03:45.130 --> 00:03:47.400 but you can read them or bookmark them 88 00:03:47.400 --> 00:03:49.790 to come back to them in the future. 89 00:03:49.790 --> 00:03:51.470 And there's quite a lot of stuff you can do. 90 00:03:51.470 --> 00:03:54.110 They are very powerful tools in Python. 91 00:03:54.110 --> 00:03:55.660 Thanks for joining me in this video. 92 00:03:55.660 --> 00:03:57.360 I hope you've learned something, and I'll see you 93 00:03:57.360 --> 00:03:58.193 in the next one.