1 00:00:00,400 --> 00:00:05,470 In previous lectures, you learned how to access a single element in a list, and in this section you 2 00:00:05,470 --> 00:00:08,710 will learning how to walk through all the elements in a list. 3 00:00:08,710 --> 00:00:14,380 So you can also work with a specific group of items in a list called a slice in Python. 4 00:00:14,380 --> 00:00:20,410 So to make a slice, you specify the index of the first and last elements you want to work with. 5 00:00:20,500 --> 00:00:26,860 As with the range function, Python stops one item before the second index. 6 00:00:26,860 --> 00:00:34,540 You specify the output of the first three elements in a list you will request here indicates root zero. 7 00:00:35,560 --> 00:00:36,160 To. 8 00:00:37,300 --> 00:00:38,080 Three here. 9 00:00:38,860 --> 00:00:48,310 So now if you write 033023 here, we will get this here and this is our slice. 10 00:00:48,550 --> 00:00:51,340 So here I will create some examples here. 11 00:00:51,340 --> 00:01:00,670 So let's again make some example about car makers or let's sometime now, Yeah, car makers is okay. 12 00:01:01,060 --> 00:01:06,760 Car makers here we will add BMW, Honda here. 13 00:01:07,570 --> 00:01:09,820 We will add Volkswagen. 14 00:01:11,070 --> 00:01:13,080 We will add the fourth. 15 00:01:14,380 --> 00:01:15,090 And I. 16 00:01:17,400 --> 00:01:18,000 Here. 17 00:01:19,020 --> 00:01:20,520 And this sun here. 18 00:01:20,520 --> 00:01:26,450 So here we will print print the zero three. 19 00:01:26,460 --> 00:01:29,550 So print carmakers. 20 00:01:31,020 --> 00:01:33,240 Carmakers zero three here. 21 00:01:33,540 --> 00:01:41,760 So this code now, let this code prints a slice of the list so the output retains the structure of a 22 00:01:41,790 --> 00:01:47,040 list and includes the first three carmakers in the list here. 23 00:01:47,040 --> 00:01:50,490 So in this case, it's going to print us here. 24 00:01:51,220 --> 00:01:58,510 It's going to print us BMW, Honda, BMW, Honda and Volkswagen. 25 00:01:58,660 --> 00:02:03,970 So it will print us these three items as we write in zero and three here. 26 00:02:04,600 --> 00:02:05,950 So now let's print this. 27 00:02:05,950 --> 00:02:08,800 And as you can see here, BMW, Honda, Volkswagen. 28 00:02:08,800 --> 00:02:12,100 So you can generate any subset of a list. 29 00:02:12,250 --> 00:02:22,120 For example, if you want to if you want to if you want to second and second, third and fourth item 30 00:02:22,120 --> 00:02:24,040 in a list, you will do this. 31 00:02:25,250 --> 00:02:25,580 Here. 32 00:02:25,580 --> 00:02:30,560 As you can see here, we started Honda, Volkswagen and Ford. 33 00:02:32,840 --> 00:02:36,140 And this time the slides start. 34 00:02:36,380 --> 00:02:40,900 Start with Honda here and ends with the Ford. 35 00:02:40,910 --> 00:02:46,970 So if you omit the first index and a slice Python automatically starts your slice at the beginning of 36 00:02:46,970 --> 00:02:47,930 the list. 37 00:02:47,960 --> 00:02:49,460 Like this here. 38 00:02:50,790 --> 00:02:52,680 You just need to add this double dot here. 39 00:02:52,680 --> 00:02:58,560 And as you can see here, we got BMW, Honda, Volkswagen and Ford. 40 00:03:00,800 --> 00:03:01,700 So. 41 00:03:02,450 --> 00:03:07,250 So a similar syntax works if you want to slice that includes the end of a list. 42 00:03:07,280 --> 00:03:14,900 So for example, if you want to, um, all items from the third item through the last item, you can 43 00:03:14,900 --> 00:03:23,390 do this two double dot, but this double dot here will have, will, will come after the number to get 44 00:03:23,390 --> 00:03:25,510 the last item. 45 00:03:25,520 --> 00:03:27,950 So here we get from the last item. 46 00:03:27,950 --> 00:03:34,130 Now in this case, we will get, as you can see here, let's actually. 47 00:03:35,410 --> 00:03:36,410 Where is my marker? 48 00:03:36,410 --> 00:03:37,060 Yes. 49 00:03:37,070 --> 00:03:40,010 So here we got Nissan. 50 00:03:40,990 --> 00:03:41,410 Kia. 51 00:03:42,220 --> 00:03:42,670 Hyundai. 52 00:03:45,650 --> 00:03:46,730 Volkswagen. 53 00:03:47,460 --> 00:03:48,540 And that's it. 54 00:03:49,360 --> 00:03:53,470 We got from here to here. 55 00:03:53,470 --> 00:03:55,840 So we got to here. 56 00:03:56,010 --> 00:03:58,300 We counted zero one second. 57 00:03:58,300 --> 00:04:03,340 And from second we told Python that get all those items from the list. 58 00:04:03,900 --> 00:04:04,720 So. 59 00:04:06,070 --> 00:04:12,250 Now Python returns all the items from the third item that through the end of the list. 60 00:04:13,510 --> 00:04:15,550 So I can afford and Nissan. 61 00:04:15,850 --> 00:04:21,340 So this syntax allows you to output all the elements from any point in your list to the end, regardless 62 00:04:21,340 --> 00:04:23,620 of the length of the list or something. 63 00:04:23,620 --> 00:04:31,000 So recall that a negative index returns an element a certain distance from the end of a list. 64 00:04:31,030 --> 00:04:34,660 Therefore, you can output any size of the end of a list. 65 00:04:34,690 --> 00:04:45,730 For example, if you want to output the last three carmakers on the carmakers, we can use this slice 66 00:04:45,910 --> 00:04:46,990 like this here. 67 00:04:49,490 --> 00:04:54,050 Minus three, negative three here and this double dot here and after that. 68 00:04:57,330 --> 00:04:58,590 We will print this. 69 00:04:58,950 --> 00:05:04,350 And as you can see here, we printed Hyundai, Kia, Nissan here. 70 00:05:04,800 --> 00:05:09,870 So you can actually include the third value in the braces indicating a slice. 71 00:05:09,870 --> 00:05:18,960 So if a third value is included, the test Python, how many items to skip between items in the specified 72 00:05:18,960 --> 00:05:19,550 range. 73 00:05:19,560 --> 00:05:26,280 So you can use a slice in a for loop if you want to loop through the subset of the elements in a list. 74 00:05:26,280 --> 00:05:35,130 So in this example, we will loop through the first three car makers and print their names as a part 75 00:05:35,130 --> 00:05:36,300 of a simple car maker. 76 00:05:36,660 --> 00:05:44,550 Now we will print here, here, here are the first. 77 00:05:47,690 --> 00:05:49,460 First for. 78 00:05:51,960 --> 00:05:55,770 For car makers in my list here. 79 00:05:55,770 --> 00:06:00,780 And after that we will use for loop for car maker. 80 00:06:00,810 --> 00:06:04,080 Car maker in car makers. 81 00:06:04,080 --> 00:06:06,690 And here we will enter this. 82 00:06:12,060 --> 00:06:12,870 Here for. 83 00:06:12,870 --> 00:06:16,470 And after that, we will play print here. 84 00:06:17,380 --> 00:06:22,660 Well print the carmaker dot title. 85 00:06:24,450 --> 00:06:26,160 So here we will. 86 00:06:26,460 --> 00:06:27,000 Let's run this. 87 00:06:27,000 --> 00:06:30,270 And as you can see here, we got this example, We got this output. 88 00:06:30,270 --> 00:06:36,960 So instead of looping through the entire list of players or car makers, Python loops through only the 89 00:06:36,960 --> 00:06:40,620 first three first for car makers. 90 00:06:40,620 --> 00:06:43,680 So slices are very useful in a number of situations. 91 00:06:43,680 --> 00:06:51,240 For instance, when you are creating a game, you could add a player, you could add a car makers model 92 00:06:51,270 --> 00:06:52,200 to a list. 93 00:06:52,200 --> 00:06:58,230 Every time that that car is running or playing, the player is playing with that car. 94 00:06:58,230 --> 00:07:00,360 So you could then get a player's. 95 00:07:01,290 --> 00:07:09,300 Top three scores by sorting the list in decreasing order and taking a slice that includes just the first 96 00:07:09,300 --> 00:07:11,130 three scores.