Often your lists will be created in an unpredictable order because you can't always control the order
in which your users provide their data.
Although this is unavoidable in most circumstances, you will frequently want to present the information
in a particular order.
Sometimes you will want to preserve the original order of your list and other times you will want to
change the original order here.
So Python provides a number of different ways to organize your lists depending on the situation.
We can also sort the list permanently with a sort method.
So let's first create a list here that we can conduct our experiments on.
In this case, it's going to be cards again.
So it's going to be Toyota or yeah, Toyota.
Audi or.
Yes.
BMW.
Audi.
Here.
Toyota.
Bar.
That's a.
And here.
So I want to talk about the sort method.
So the sort method makes it relatively easy to sort a list.
Imagine we have a list of cars and we want to change the order of the list to store them alphabetically.
To keep this task simple, let's assume that all the values in the list are lowercase.
Now, actually, instead of assuming this, actually use this lowercase method and we can use the title
or uppercase methods.
So make so good.
So here we will use the cars that sort.
And we will after that.
Print.
Cause That's it.
And as you can see here, A, b, s t here.
So the sort method changes the order of the list permanently.
The cars are now in alphabetical order and we can never revert to the original order here.
So you can also sort the list in reverse alphabetical order by passing the argument reverse true to
the sort method.
So here we in order to do that, we will use reverse true.
And here.
So the first example was, uh, here, let me fix my mouse right now.
Yes.
So the first example was the, our first example was.
This is the row here that we entered, which is the first was BMW.
We.
M-W then Audi.
And then Toyota.
And then Subaru.
This is the first row here.
And then with sort.
Let's actually use a different pen.
And here with salt here.
I'm sorry.
And after that, here we have Audi, BMW, Subaru and Toyota.
So we will delete this.
And now we will create a new sort here.
Car, that sort.
Reverse.
True.
And print cars again.
That's it.
And this is how it works.
Firstly, we sorted the cars out.
The BMW is here and as you can see in alphabetical order, it's the Scandic, right?
A, B, C, so TSB, A and a, B, C.
We can also sort the list temporarily with the sorted function so the maintain the original order of
the list but present it in a sorted order.
You can use the sorted function.
The sorted function lets you display your list in a particular order but doesn't affect the actual order
of the list.
So let's try this function on the list of our cars.
So here.
We will use assorted cars, cars of assorted cars.
And here, let's print this.
Cars.
And as you can see, it's the same.
But now look at this.
In order to save this sort.
So in order to make it permanently, you will do cars equals sorted cars and this is how you make it
permanent.
So here we will write.
Print.
Sorted.
Sorted cars.
I'm.
And here we will print the cars again.
And as I said, this is temporarily.
That's why it changed backwards here, the original here.
So we first here, we point the original order and then in alphabetical order.
And after the list is displayed at the new order, we show that the list is still stored in its original
order.
We can also print a list in reverse order.
So to reverse the original order of layers, you can use the reverse method.
So if we originally stored the list of cars in chronological order according to when we owned them,
we could easily rearrange the list into reverse chronological order.
In order to do that, we will use first print actually cars.
So we will see and hear after that.
Cars dot reverse.
And after that print cars.
That's it.
We've also entered the Subaru.
So here we have Subaru, Toyota of.
Toyota, Audi and BMW.
That's it.
And notice that the reverse doesn't sort backward alpha tab alphabetically.
It simply reverses the order of the list and the reverse method change the order of a list permanently,
but you can revert to original order anytime by applying reverse to the same list a second time.
Let's try this now.
Cars dot reverse and print.
That's it.
And here, as you can see here, this is the original original, but it's two times reverse here.
We can also find the length of list here so you can quickly find the length of list by using the Len
function.
The list in the list in this example has four items, so its length is four.
So now we will.
BMW here.
Oh, the.
We are created.
Sorry.
And then here.
Print, Len.
Cars.
And thus you can see here it showed us for.