WEBVTT

00:00.400 --> 00:05.470
In previous lectures, you learned how to access a single element in a list, and in this section you

00:05.470 --> 00:08.710
will learning how to walk through all the elements in a list.

00:08.710 --> 00:14.380
So you can also work with a specific group of items in a list called a slice in Python.

00:14.380 --> 00:20.410
So to make a slice, you specify the index of the first and last elements you want to work with.

00:20.500 --> 00:26.860
As with the range function, Python stops one item before the second index.

00:26.860 --> 00:34.540
You specify the output of the first three elements in a list you will request here indicates root zero.

00:35.560 --> 00:36.160
To.

00:37.300 --> 00:38.080
Three here.

00:38.860 --> 00:48.310
So now if you write 033023 here, we will get this here and this is our slice.

00:48.550 --> 00:51.340
So here I will create some examples here.

00:51.340 --> 01:00.670
So let's again make some example about car makers or let's sometime now, Yeah, car makers is okay.

01:01.060 --> 01:06.760
Car makers here we will add BMW, Honda here.

01:07.570 --> 01:09.820
We will add Volkswagen.

01:11.070 --> 01:13.080
We will add the fourth.

01:14.380 --> 01:15.090
And I.

01:17.400 --> 01:18.000
Here.

01:19.020 --> 01:20.520
And this sun here.

01:20.520 --> 01:26.450
So here we will print print the zero three.

01:26.460 --> 01:29.550
So print carmakers.

01:31.020 --> 01:33.240
Carmakers zero three here.

01:33.540 --> 01:41.760
So this code now, let this code prints a slice of the list so the output retains the structure of a

01:41.790 --> 01:47.040
list and includes the first three carmakers in the list here.

01:47.040 --> 01:50.490
So in this case, it's going to print us here.

01:51.220 --> 01:58.510
It's going to print us BMW, Honda, BMW, Honda and Volkswagen.

01:58.660 --> 02:03.970
So it will print us these three items as we write in zero and three here.

02:04.600 --> 02:05.950
So now let's print this.

02:05.950 --> 02:08.800
And as you can see here, BMW, Honda, Volkswagen.

02:08.800 --> 02:12.100
So you can generate any subset of a list.

02:12.250 --> 02:22.120
For example, if you want to if you want to if you want to second and second, third and fourth item

02:22.120 --> 02:24.040
in a list, you will do this.

02:25.250 --> 02:25.580
Here.

02:25.580 --> 02:30.560
As you can see here, we started Honda, Volkswagen and Ford.

02:32.840 --> 02:36.140
And this time the slides start.

02:36.380 --> 02:40.900
Start with Honda here and ends with the Ford.

02:40.910 --> 02:46.970
So if you omit the first index and a slice Python automatically starts your slice at the beginning of

02:46.970 --> 02:47.930
the list.

02:47.960 --> 02:49.460
Like this here.

02:50.790 --> 02:52.680
You just need to add this double dot here.

02:52.680 --> 02:58.560
And as you can see here, we got BMW, Honda, Volkswagen and Ford.

03:00.800 --> 03:01.700
So.

03:02.450 --> 03:07.250
So a similar syntax works if you want to slice that includes the end of a list.

03:07.280 --> 03:14.900
So for example, if you want to, um, all items from the third item through the last item, you can

03:14.900 --> 03:23.390
do this two double dot, but this double dot here will have, will, will come after the number to get

03:23.390 --> 03:25.510
the last item.

03:25.520 --> 03:27.950
So here we get from the last item.

03:27.950 --> 03:34.130
Now in this case, we will get, as you can see here, let's actually.

03:35.410 --> 03:36.410
Where is my marker?

03:36.410 --> 03:37.060
Yes.

03:37.070 --> 03:40.010
So here we got Nissan.

03:40.990 --> 03:41.410
Kia.

03:42.220 --> 03:42.670
Hyundai.

03:45.650 --> 03:46.730
Volkswagen.

03:47.460 --> 03:48.540
And that's it.

03:49.360 --> 03:53.470
We got from here to here.

03:53.470 --> 03:55.840
So we got to here.

03:56.010 --> 03:58.300
We counted zero one second.

03:58.300 --> 04:03.340
And from second we told Python that get all those items from the list.

04:03.900 --> 04:04.720
So.

04:06.070 --> 04:12.250
Now Python returns all the items from the third item that through the end of the list.

04:13.510 --> 04:15.550
So I can afford and Nissan.

04:15.850 --> 04:21.340
So this syntax allows you to output all the elements from any point in your list to the end, regardless

04:21.340 --> 04:23.620
of the length of the list or something.

04:23.620 --> 04:31.000
So recall that a negative index returns an element a certain distance from the end of a list.

04:31.030 --> 04:34.660
Therefore, you can output any size of the end of a list.

04:34.690 --> 04:45.730
For example, if you want to output the last three carmakers on the carmakers, we can use this slice

04:45.910 --> 04:46.990
like this here.

04:49.490 --> 04:54.050
Minus three, negative three here and this double dot here and after that.

04:57.330 --> 04:58.590
We will print this.

04:58.950 --> 05:04.350
And as you can see here, we printed Hyundai, Kia, Nissan here.

05:04.800 --> 05:09.870
So you can actually include the third value in the braces indicating a slice.

05:09.870 --> 05:18.960
So if a third value is included, the test Python, how many items to skip between items in the specified

05:18.960 --> 05:19.550
range.

05:19.560 --> 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.

05:26.280 --> 05:35.130
So in this example, we will loop through the first three car makers and print their names as a part

05:35.130 --> 05:36.300
of a simple car maker.

05:36.660 --> 05:44.550
Now we will print here, here, here are the first.

05:47.690 --> 05:49.460
First for.

05:51.960 --> 05:55.770
For car makers in my list here.

05:55.770 --> 06:00.780
And after that we will use for loop for car maker.

06:00.810 --> 06:04.080
Car maker in car makers.

06:04.080 --> 06:06.690
And here we will enter this.

06:12.060 --> 06:12.870
Here for.

06:12.870 --> 06:16.470
And after that, we will play print here.

06:17.380 --> 06:22.660
Well print the carmaker dot title.

06:24.450 --> 06:26.160
So here we will.

06:26.460 --> 06:27.000
Let's run this.

06:27.000 --> 06:30.270
And as you can see here, we got this example, We got this output.

06:30.270 --> 06:36.960
So instead of looping through the entire list of players or car makers, Python loops through only the

06:36.960 --> 06:40.620
first three first for car makers.

06:40.620 --> 06:43.680
So slices are very useful in a number of situations.

06:43.680 --> 06:51.240
For instance, when you are creating a game, you could add a player, you could add a car makers model

06:51.270 --> 06:52.200
to a list.

06:52.200 --> 06:58.230
Every time that that car is running or playing, the player is playing with that car.

06:58.230 --> 07:00.360
So you could then get a player's.

07:01.290 --> 07:09.300
Top three scores by sorting the list in decreasing order and taking a slice that includes just the first

07:09.300 --> 07:11.130
three scores.
