In previous lectures, you learned how to access a single element in a list, and in this section you
will learning how to walk through all the elements in a list.
So you can also work with a specific group of items in a list called a slice in Python.
So to make a slice, you specify the index of the first and last elements you want to work with.
As with the range function, Python stops one item before the second index.
You specify the output of the first three elements in a list you will request here indicates root zero.
To.
Three here.
So now if you write 033023 here, we will get this here and this is our slice.
So here I will create some examples here.
So let's again make some example about car makers or let's sometime now, Yeah, car makers is okay.
Car makers here we will add BMW, Honda here.
We will add Volkswagen.
We will add the fourth.
And I.
Here.
And this sun here.
So here we will print print the zero three.
So print carmakers.
Carmakers zero three here.
So this code now, let this code prints a slice of the list so the output retains the structure of a
list and includes the first three carmakers in the list here.
So in this case, it's going to print us here.
It's going to print us BMW, Honda, BMW, Honda and Volkswagen.
So it will print us these three items as we write in zero and three here.
So now let's print this.
And as you can see here, BMW, Honda, Volkswagen.
So you can generate any subset of a list.
For example, if you want to if you want to if you want to second and second, third and fourth item
in a list, you will do this.
Here.
As you can see here, we started Honda, Volkswagen and Ford.
And this time the slides start.
Start with Honda here and ends with the Ford.
So if you omit the first index and a slice Python automatically starts your slice at the beginning of
the list.
Like this here.
You just need to add this double dot here.
And as you can see here, we got BMW, Honda, Volkswagen and Ford.
So.
So a similar syntax works if you want to slice that includes the end of a list.
So for example, if you want to, um, all items from the third item through the last item, you can
do this two double dot, but this double dot here will have, will, will come after the number to get
the last item.
So here we get from the last item.
Now in this case, we will get, as you can see here, let's actually.
Where is my marker?
Yes.
So here we got Nissan.
Kia.
Hyundai.
Volkswagen.
And that's it.
We got from here to here.
So we got to here.
We counted zero one second.
And from second we told Python that get all those items from the list.
So.
Now Python returns all the items from the third item that through the end of the list.
So I can afford and Nissan.
So this syntax allows you to output all the elements from any point in your list to the end, regardless
of the length of the list or something.
So recall that a negative index returns an element a certain distance from the end of a list.
Therefore, you can output any size of the end of a list.
For example, if you want to output the last three carmakers on the carmakers, we can use this slice
like this here.
Minus three, negative three here and this double dot here and after that.
We will print this.
And as you can see here, we printed Hyundai, Kia, Nissan here.
So you can actually include the third value in the braces indicating a slice.
So if a third value is included, the test Python, how many items to skip between items in the specified
range.
So you can use a slice in a for loop if you want to loop through the subset of the elements in a list.
So in this example, we will loop through the first three car makers and print their names as a part
of a simple car maker.
Now we will print here, here, here are the first.
First for.
For car makers in my list here.
And after that we will use for loop for car maker.
Car maker in car makers.
And here we will enter this.
Here for.
And after that, we will play print here.
Well print the carmaker dot title.
So here we will.
Let's run this.
And as you can see here, we got this example, We got this output.
So instead of looping through the entire list of players or car makers, Python loops through only the
first three first for car makers.
So slices are very useful in a number of situations.
For instance, when you are creating a game, you could add a player, you could add a car makers model
to a list.
Every time that that car is running or playing, the player is playing with that car.
So you could then get a player's.
Top three scores by sorting the list in decreasing order and taking a slice that includes just the first
three scores.