Next up, simple exercise to get some practice with the basics of lists.
So it's themed around a Formula One Grand Prix, but doesn't really matter.
It's just going to be some basic instructions you follow in the starter code file.
So download that and then open it up.
I've given you a list that has what, six different people in it Charles, Pierre, Valtteri, Lewis,
George and Lando.
And then what you need to do is just follow the comments, tells you, you know, correct this spelling
or add this person or people or remove this or clear the list to loop over the list.
Whatever you do, though, make sure you're doing it after the creation.
So don't you know if it says like, change the spelling of Valtteri, don't just go in here and change.
It kind of defeats the purpose.
Imagine you don't have access to this.
You have to do it after the fact.
That's the whole point of the exercise.
Okay, so step one, it's race day in our starting British set.
Charles sits on pole and Lando brings up the rear.
Okay, great.
Set the scene there.
Oh, dear.
We've misspelled Valtteri with two T's as Valtteri with two RS.
Update the driver's list to include the correct spelling.
So we need to change this element which has an index of 012.
So drivers of two is now going to be the correct spelling.
Valtteri with two t's.
Simple enough.
Esteban makes it out of the pits and joins the pack just in time.
Add Esteban to the end of the list.
He joins at the back of the pack.
So we use append drivers, append Esteban.
So now at the end of drivers, we've got Esteban.
I guess we're not printing anything out.
We don't really know what it looks like.
But this is correct.
What's this?
There's another group of drivers that comes out of nowhere to join the race.
Add each element from the others list to the end of the drivers list.
And I'd like you to do this with a single method call.
So the easiest way is to use the extend method.
So drivers that extend.
And this takes an iterable like others.
What does it do?
It iterates over others, and it depends each element to the end of drivers.
Okay.
So I guess at this point, we could just take a look at what we're dealing with if we wanted to.
Why don't I just print drivers out at the end?
And there we go.
We see Valter has been corrected.
We see Esteban was added on.
And then Blue Elton and Colt were all added to the end with a single line.
All right, Colt.
Looks like our Colt looks lost out there.
Yeah.
I'm not a kid, not a good F1 driver.
I have to admit, he has a horrible, fiery crash.
Remove the last element from the driver's list, which is Colt.
So there's multiple ways of doing this, but the easiest one is just pop and that's it.
DRIVER Stop.
Pop If we print it out now, I'm no longer at the end of the list.
Now, Elton is all right.
There's a huge crash at the front.
Remove the first element from the driver's list.
So how do we remove from the beginning?
The easiest way is to do another pop driver step pop and provide an index like zero.
That would be, you know, the beginning of the list.
So that should remove Charles.
He's no longer at the front.
Now it's Pierre.
Sorry, Bud.
And again, the leader of the pack runs into trouble.
He's not out of the race, but now he's moved to last place.
Remove the first driver in the list, store it in a variable, and then add it to the end of the list
so we can repeat what we just did.
We can pop from the beginning and that returns the element, right?
So that will return whoever was popped off.
And then we can take that and append it to the end.
So maybe we'll call this the leader.
And what append leader?
So we took whoever was at the front, which was Pierre, and then we removed him and appended him to
the end.
Okay.
My idiotic cat blue is in the middle of the pack.
Delete her from the driver's list using the remove method.
So it's as simple as drivers.
Stop, remove.
And then we specify not the position but the value we're trying to remove and it has to match the exact
spelling.
Capital B Blue Run.
This blue used to be after Esteban.
Now she's gone.
Sorry, blue.
My dog, Elton, is making a remarkable charge up the leader board.
Remove him from his current position in the list and insert him at index two.
So let's remove Elton.
Driver, stop.
Remove Elton and then we're going to add Elton in that index of two.
So that's drivers that lets do insert to insert an index to the string.
Elton And let's print drivers out and we should now see.
Elton has moved from almost at the back to index of 2012.
And then the race is now over.
It's time for the podium ceremony.
Create a new list called Podium that contains the first three elements from the drivers list.
Use a slice.
So if we want to go from zero to index of three.
It doesn't include index of three.
So that would be 012.
But the shorter syntax is just colon three, print podium.
And we should see not podium podium.
There we are.
Valtteri.
Lewis and Elton.
Our podium.
And finally, loop over the driver's list and print out a leaderboard.
So all the drivers, not just the podium that include the drivers finishing position and their name
like this.
So there's a number, a dot period and then their name.
So it's not as simple as just doing a four, you know, driver in drivers because we need a number.
So this will print out.
If I just did print driver, this would just, you know, print the driver and we see each one in order.
But how do I get that number?
There's a bunch of ways, but what I think is probably easiest is to use a for loop and we're going
to loop for NUM in the range length of drivers.
So whatever the length of the drivers is, it's seven rates we're basically doing in range of seven.
That's just going to give us a number that goes from zero up to six and then we can use that.
So I could start I'm going to just go back to having Len drivers.
We'll just start by printing drivers, square brackets.
No, and this will just replicate what we already had.
Valtteri Lewis We're not using that number, but if I print that number out, I'll just do a comma.
So num comma, drivers of num we're getting that number and we can use it.
So I'm using that to access the driver out of the list.
But then if I add one to it, I can get the numeral right there finishing position.
So I'm going to change this and print an F string.
In this F string, I'm going to insert the number plus one with a dot afterwards and then a space and
then the drivers list square brackets.
No.
So whatever that index is.
So instead of num, this could be id x if we prefer that.
Just to make it clear it's an index, right?
We're accessing that index or using the index to access a driver out and then adding one to get a number
that starts with one set of zero.
And there we go.
One Valtteri, two Lewis, three Elton.
Great job for a dog, seriously.
And that is it for this little lists Grand Prix.
Okay.