1 00:00:01,380 --> 00:00:07,320 Often your lists will be created in an unpredictable order because you can't always control the order 2 00:00:07,320 --> 00:00:09,090 in which your users provide their data. 3 00:00:09,120 --> 00:00:15,180 Although this is unavoidable in most circumstances, you will frequently want to present the information 4 00:00:15,180 --> 00:00:16,490 in a particular order. 5 00:00:16,500 --> 00:00:21,690 Sometimes you will want to preserve the original order of your list and other times you will want to 6 00:00:21,690 --> 00:00:23,790 change the original order here. 7 00:00:23,790 --> 00:00:30,000 So Python provides a number of different ways to organize your lists depending on the situation. 8 00:00:30,870 --> 00:00:34,230 We can also sort the list permanently with a sort method. 9 00:00:34,470 --> 00:00:41,280 So let's first create a list here that we can conduct our experiments on. 10 00:00:41,610 --> 00:00:43,920 In this case, it's going to be cards again. 11 00:00:43,920 --> 00:00:48,150 So it's going to be Toyota or yeah, Toyota. 12 00:00:50,360 --> 00:00:51,620 Audi or. 13 00:00:51,620 --> 00:00:52,280 Yes. 14 00:00:52,280 --> 00:00:52,610 Yes. 15 00:00:53,210 --> 00:00:54,260 BMW. 16 00:00:55,220 --> 00:00:55,670 Audi. 17 00:00:57,960 --> 00:00:58,740 Audi. 18 00:01:01,080 --> 00:01:01,590 Here. 19 00:01:04,550 --> 00:01:05,030 Toyota. 20 00:01:07,460 --> 00:01:07,790 Bar. 21 00:01:11,980 --> 00:01:12,430 That's a. 22 00:01:18,090 --> 00:01:19,200 And here. 23 00:01:19,650 --> 00:01:23,370 So I want to talk about the sort method. 24 00:01:23,370 --> 00:01:28,600 So the sort method makes it relatively easy to sort a list. 25 00:01:28,620 --> 00:01:34,540 Imagine we have a list of cars and we want to change the order of the list to store them alphabetically. 26 00:01:34,560 --> 00:01:41,790 To keep this task simple, let's assume that all the values in the list are lowercase. 27 00:01:41,880 --> 00:01:51,030 Now, actually, instead of assuming this, actually use this lowercase method and we can use the title 28 00:01:51,030 --> 00:01:52,500 or uppercase methods. 29 00:01:53,410 --> 00:01:55,360 So make so good. 30 00:01:55,450 --> 00:02:00,250 So here we will use the cars that sort. 31 00:02:02,020 --> 00:02:03,550 And we will after that. 32 00:02:06,610 --> 00:02:07,240 Print. 33 00:02:07,990 --> 00:02:09,580 Cause That's it. 34 00:02:10,740 --> 00:02:14,220 And as you can see here, A, b, s t here. 35 00:02:14,430 --> 00:02:18,660 So the sort method changes the order of the list permanently. 36 00:02:18,690 --> 00:02:26,310 The cars are now in alphabetical order and we can never revert to the original order here. 37 00:02:26,310 --> 00:02:32,910 So you can also sort the list in reverse alphabetical order by passing the argument reverse true to 38 00:02:32,910 --> 00:02:34,050 the sort method. 39 00:02:34,200 --> 00:02:39,300 So here we in order to do that, we will use reverse true. 40 00:02:39,300 --> 00:02:40,530 And here. 41 00:02:40,530 --> 00:02:46,380 So the first example was, uh, here, let me fix my mouse right now. 42 00:02:46,380 --> 00:02:46,950 Yes. 43 00:02:46,950 --> 00:02:51,630 So the first example was the, our first example was. 44 00:02:59,520 --> 00:03:05,490 This is the row here that we entered, which is the first was BMW. 45 00:03:06,000 --> 00:03:06,660 We. 46 00:03:07,510 --> 00:03:09,820 M-W then Audi. 47 00:03:12,850 --> 00:03:14,020 And then Toyota. 48 00:03:19,140 --> 00:03:20,370 And then Subaru. 49 00:03:28,510 --> 00:03:30,490 This is the first row here. 50 00:03:31,090 --> 00:03:34,060 And then with sort. 51 00:03:35,520 --> 00:03:37,200 Let's actually use a different pen. 52 00:03:42,830 --> 00:03:44,720 And here with salt here. 53 00:03:53,860 --> 00:03:54,550 I'm sorry. 54 00:04:01,880 --> 00:04:06,590 And after that, here we have Audi, BMW, Subaru and Toyota. 55 00:04:08,050 --> 00:04:10,930 So we will delete this. 56 00:04:12,270 --> 00:04:14,910 And now we will create a new sort here. 57 00:04:15,810 --> 00:04:17,430 Car, that sort. 58 00:04:20,350 --> 00:04:21,400 Reverse. 59 00:04:21,400 --> 00:04:22,240 True. 60 00:04:23,780 --> 00:04:25,700 And print cars again. 61 00:04:27,120 --> 00:04:27,840 That's it. 62 00:04:29,660 --> 00:04:31,130 And this is how it works. 63 00:04:31,160 --> 00:04:33,310 Firstly, we sorted the cars out. 64 00:04:33,320 --> 00:04:39,170 The BMW is here and as you can see in alphabetical order, it's the Scandic, right? 65 00:04:39,290 --> 00:04:43,310 A, B, C, so TSB, A and a, B, C. 66 00:04:44,240 --> 00:04:49,550 We can also sort the list temporarily with the sorted function so the maintain the original order of 67 00:04:49,550 --> 00:04:52,160 the list but present it in a sorted order. 68 00:04:54,560 --> 00:04:56,290 You can use the sorted function. 69 00:04:56,300 --> 00:05:02,540 The sorted function lets you display your list in a particular order but doesn't affect the actual order 70 00:05:02,540 --> 00:05:03,600 of the list. 71 00:05:03,620 --> 00:05:07,790 So let's try this function on the list of our cars. 72 00:05:07,790 --> 00:05:08,990 So here. 73 00:05:10,190 --> 00:05:17,510 We will use assorted cars, cars of assorted cars. 74 00:05:17,510 --> 00:05:20,270 And here, let's print this. 75 00:05:20,300 --> 00:05:21,080 Cars. 76 00:05:21,970 --> 00:05:24,280 And as you can see, it's the same. 77 00:05:24,280 --> 00:05:27,040 But now look at this. 78 00:05:27,370 --> 00:05:29,500 In order to save this sort. 79 00:05:29,500 --> 00:05:36,100 So in order to make it permanently, you will do cars equals sorted cars and this is how you make it 80 00:05:36,100 --> 00:05:36,820 permanent. 81 00:05:37,150 --> 00:05:41,350 So here we will write. 82 00:05:41,380 --> 00:05:42,430 Print. 83 00:05:43,590 --> 00:05:44,520 Print. 84 00:05:45,410 --> 00:05:46,340 Sorted. 85 00:05:49,230 --> 00:05:51,350 Sorted cars. 86 00:05:52,560 --> 00:05:52,710 I'm. 87 00:05:55,760 --> 00:05:58,400 And here we will print the cars again. 88 00:05:59,720 --> 00:06:01,820 And as I said, this is temporarily. 89 00:06:01,820 --> 00:06:06,140 That's why it changed backwards here, the original here. 90 00:06:06,530 --> 00:06:13,220 So we first here, we point the original order and then in alphabetical order. 91 00:06:13,220 --> 00:06:20,030 And after the list is displayed at the new order, we show that the list is still stored in its original 92 00:06:20,030 --> 00:06:20,770 order. 93 00:06:20,780 --> 00:06:24,590 We can also print a list in reverse order. 94 00:06:25,420 --> 00:06:30,780 So to reverse the original order of layers, you can use the reverse method. 95 00:06:30,790 --> 00:06:37,150 So if we originally stored the list of cars in chronological order according to when we owned them, 96 00:06:37,150 --> 00:06:42,400 we could easily rearrange the list into reverse chronological order. 97 00:06:42,430 --> 00:06:45,610 In order to do that, we will use first print actually cars. 98 00:06:45,610 --> 00:06:49,120 So we will see and hear after that. 99 00:06:49,120 --> 00:06:51,280 Cars dot reverse. 100 00:06:52,810 --> 00:06:54,280 And after that print cars. 101 00:06:59,510 --> 00:07:00,320 That's it. 102 00:07:00,350 --> 00:07:02,270 We've also entered the Subaru. 103 00:07:02,840 --> 00:07:06,820 So here we have Subaru, Toyota of. 104 00:07:08,360 --> 00:07:13,280 Toyota, Audi and BMW. 105 00:07:14,630 --> 00:07:15,230 That's it. 106 00:07:16,320 --> 00:07:21,510 And notice that the reverse doesn't sort backward alpha tab alphabetically. 107 00:07:21,510 --> 00:07:27,090 It simply reverses the order of the list and the reverse method change the order of a list permanently, 108 00:07:27,090 --> 00:07:35,760 but you can revert to original order anytime by applying reverse to the same list a second time. 109 00:07:35,760 --> 00:07:37,350 Let's try this now. 110 00:07:37,470 --> 00:07:41,670 Cars dot reverse and print. 111 00:07:43,420 --> 00:07:44,050 That's it. 112 00:07:44,350 --> 00:07:50,470 And here, as you can see here, this is the original original, but it's two times reverse here. 113 00:07:51,040 --> 00: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 114 00:07:57,400 --> 00:07:57,910 function. 115 00:07:57,910 --> 00:08:03,340 The list in the list in this example has four items, so its length is four. 116 00:08:03,640 --> 00:08:06,490 So now we will. 117 00:08:10,170 --> 00:08:11,490 BMW here. 118 00:08:12,820 --> 00:08:13,240 Oh, the. 119 00:08:13,600 --> 00:08:14,470 We are created. 120 00:08:14,470 --> 00:08:15,040 Sorry. 121 00:08:15,400 --> 00:08:16,690 And then here. 122 00:08:16,690 --> 00:08:18,340 Print, Len. 123 00:08:20,550 --> 00:08:21,180 Cars. 124 00:08:23,270 --> 00:08:26,180 And thus you can see here it showed us for.