WEBVTT

00:01.380 --> 00:07.320
Often your lists will be created in an unpredictable order because you can't always control the order

00:07.320 --> 00:09.090
in which your users provide their data.

00:09.120 --> 00:15.180
Although this is unavoidable in most circumstances, you will frequently want to present the information

00:15.180 --> 00:16.490
in a particular order.

00:16.500 --> 00:21.690
Sometimes you will want to preserve the original order of your list and other times you will want to

00:21.690 --> 00:23.790
change the original order here.

00:23.790 --> 00:30.000
So Python provides a number of different ways to organize your lists depending on the situation.

00:30.870 --> 00:34.230
We can also sort the list permanently with a sort method.

00:34.470 --> 00:41.280
So let's first create a list here that we can conduct our experiments on.

00:41.610 --> 00:43.920
In this case, it's going to be cards again.

00:43.920 --> 00:48.150
So it's going to be Toyota or yeah, Toyota.

00:50.360 --> 00:51.620
Audi or.

00:51.620 --> 00:52.280
Yes.

00:52.280 --> 00:52.610
Yes.

00:53.210 --> 00:54.260
BMW.

00:55.220 --> 00:55.670
Audi.

00:57.960 --> 00:58.740
Audi.

01:01.080 --> 01:01.590
Here.

01:04.550 --> 01:05.030
Toyota.

01:07.460 --> 01:07.790
Bar.

01:11.980 --> 01:12.430
That's a.

01:18.090 --> 01:19.200
And here.

01:19.650 --> 01:23.370
So I want to talk about the sort method.

01:23.370 --> 01:28.600
So the sort method makes it relatively easy to sort a list.

01:28.620 --> 01:34.540
Imagine we have a list of cars and we want to change the order of the list to store them alphabetically.

01:34.560 --> 01:41.790
To keep this task simple, let's assume that all the values in the list are lowercase.

01:41.880 --> 01:51.030
Now, actually, instead of assuming this, actually use this lowercase method and we can use the title

01:51.030 --> 01:52.500
or uppercase methods.

01:53.410 --> 01:55.360
So make so good.

01:55.450 --> 02:00.250
So here we will use the cars that sort.

02:02.020 --> 02:03.550
And we will after that.

02:06.610 --> 02:07.240
Print.

02:07.990 --> 02:09.580
Cause That's it.

02:10.740 --> 02:14.220
And as you can see here, A, b, s t here.

02:14.430 --> 02:18.660
So the sort method changes the order of the list permanently.

02:18.690 --> 02:26.310
The cars are now in alphabetical order and we can never revert to the original order here.

02:26.310 --> 02:32.910
So you can also sort the list in reverse alphabetical order by passing the argument reverse true to

02:32.910 --> 02:34.050
the sort method.

02:34.200 --> 02:39.300
So here we in order to do that, we will use reverse true.

02:39.300 --> 02:40.530
And here.

02:40.530 --> 02:46.380
So the first example was, uh, here, let me fix my mouse right now.

02:46.380 --> 02:46.950
Yes.

02:46.950 --> 02:51.630
So the first example was the, our first example was.

02:59.520 --> 03:05.490
This is the row here that we entered, which is the first was BMW.

03:06.000 --> 03:06.660
We.

03:07.510 --> 03:09.820
M-W then Audi.

03:12.850 --> 03:14.020
And then Toyota.

03:19.140 --> 03:20.370
And then Subaru.

03:28.510 --> 03:30.490
This is the first row here.

03:31.090 --> 03:34.060
And then with sort.

03:35.520 --> 03:37.200
Let's actually use a different pen.

03:42.830 --> 03:44.720
And here with salt here.

03:53.860 --> 03:54.550
I'm sorry.

04:01.880 --> 04:06.590
And after that, here we have Audi, BMW, Subaru and Toyota.

04:08.050 --> 04:10.930
So we will delete this.

04:12.270 --> 04:14.910
And now we will create a new sort here.

04:15.810 --> 04:17.430
Car, that sort.

04:20.350 --> 04:21.400
Reverse.

04:21.400 --> 04:22.240
True.

04:23.780 --> 04:25.700
And print cars again.

04:27.120 --> 04:27.840
That's it.

04:29.660 --> 04:31.130
And this is how it works.

04:31.160 --> 04:33.310
Firstly, we sorted the cars out.

04:33.320 --> 04:39.170
The BMW is here and as you can see in alphabetical order, it's the Scandic, right?

04:39.290 --> 04:43.310
A, B, C, so TSB, A and a, B, C.

04:44.240 --> 04:49.550
We can also sort the list temporarily with the sorted function so the maintain the original order of

04:49.550 --> 04:52.160
the list but present it in a sorted order.

04:54.560 --> 04:56.290
You can use the sorted function.

04:56.300 --> 05:02.540
The sorted function lets you display your list in a particular order but doesn't affect the actual order

05:02.540 --> 05:03.600
of the list.

05:03.620 --> 05:07.790
So let's try this function on the list of our cars.

05:07.790 --> 05:08.990
So here.

05:10.190 --> 05:17.510
We will use assorted cars, cars of assorted cars.

05:17.510 --> 05:20.270
And here, let's print this.

05:20.300 --> 05:21.080
Cars.

05:21.970 --> 05:24.280
And as you can see, it's the same.

05:24.280 --> 05:27.040
But now look at this.

05:27.370 --> 05:29.500
In order to save this sort.

05:29.500 --> 05:36.100
So in order to make it permanently, you will do cars equals sorted cars and this is how you make it

05:36.100 --> 05:36.820
permanent.

05:37.150 --> 05:41.350
So here we will write.

05:41.380 --> 05:42.430
Print.

05:43.590 --> 05:44.520
Print.

05:45.410 --> 05:46.340
Sorted.

05:49.230 --> 05:51.350
Sorted cars.

05:52.560 --> 05:52.710
I'm.

05:55.760 --> 05:58.400
And here we will print the cars again.

05:59.720 --> 06:01.820
And as I said, this is temporarily.

06:01.820 --> 06:06.140
That's why it changed backwards here, the original here.

06:06.530 --> 06:13.220
So we first here, we point the original order and then in alphabetical order.

06:13.220 --> 06:20.030
And after the list is displayed at the new order, we show that the list is still stored in its original

06:20.030 --> 06:20.770
order.

06:20.780 --> 06:24.590
We can also print a list in reverse order.

06:25.420 --> 06:30.780
So to reverse the original order of layers, you can use the reverse method.

06:30.790 --> 06:37.150
So if we originally stored the list of cars in chronological order according to when we owned them,

06:37.150 --> 06:42.400
we could easily rearrange the list into reverse chronological order.

06:42.430 --> 06:45.610
In order to do that, we will use first print actually cars.

06:45.610 --> 06:49.120
So we will see and hear after that.

06:49.120 --> 06:51.280
Cars dot reverse.

06:52.810 --> 06:54.280
And after that print cars.

06:59.510 --> 07:00.320
That's it.

07:00.350 --> 07:02.270
We've also entered the Subaru.

07:02.840 --> 07:06.820
So here we have Subaru, Toyota of.

07:08.360 --> 07:13.280
Toyota, Audi and BMW.

07:14.630 --> 07:15.230
That's it.

07:16.320 --> 07:21.510
And notice that the reverse doesn't sort backward alpha tab alphabetically.

07:21.510 --> 07:27.090
It simply reverses the order of the list and the reverse method change the order of a list permanently,

07:27.090 --> 07:35.760
but you can revert to original order anytime by applying reverse to the same list a second time.

07:35.760 --> 07:37.350
Let's try this now.

07:37.470 --> 07:41.670
Cars dot reverse and print.

07:43.420 --> 07:44.050
That's it.

07:44.350 --> 07:50.470
And here, as you can see here, this is the original original, but it's two times reverse here.

07:51.040 --> 07:57.390
We can also find the length of list here so you can quickly find the length of list by using the Len

07:57.400 --> 07:57.910
function.

07:57.910 --> 08:03.340
The list in the list in this example has four items, so its length is four.

08:03.640 --> 08:06.490
So now we will.

08:10.170 --> 08:11.490
BMW here.

08:12.820 --> 08:13.240
Oh, the.

08:13.600 --> 08:14.470
We are created.

08:14.470 --> 08:15.040
Sorry.

08:15.400 --> 08:16.690
And then here.

08:16.690 --> 08:18.340
Print, Len.

08:20.550 --> 08:21.180
Cars.

08:23.270 --> 08:26.180
And thus you can see here it showed us for.
