1 00:00:00,750 --> 00:00:01,930 - [Narrator] In this video, I'd like to 2 00:00:01,930 --> 00:00:05,300 take a a look at sorting the elements of a list 3 00:00:05,300 --> 00:00:08,370 and then showing you another way to do sorting, 4 00:00:08,370 --> 00:00:10,950 which will work not only with lists, 5 00:00:10,950 --> 00:00:13,460 but also with the contents of a string 6 00:00:13,460 --> 00:00:15,910 or with tuples as well. 7 00:00:15,910 --> 00:00:18,610 So for the purpose of this example to start out, 8 00:00:18,610 --> 00:00:21,020 let's create this list called numbers. 9 00:00:21,020 --> 00:00:23,960 And as you can see, I've purposely created the list 10 00:00:23,960 --> 00:00:26,470 out of order, so that we can demonstrate 11 00:00:26,470 --> 00:00:28,430 sorting capabilities. 12 00:00:28,430 --> 00:00:31,650 Now, it turns out that built in to the list type 13 00:00:31,650 --> 00:00:35,360 is a sort method that allows you to modify 14 00:00:35,360 --> 00:00:36,500 a list in place. 15 00:00:36,500 --> 00:00:39,240 So if I type numbers, and a dot, and tab, 16 00:00:39,240 --> 00:00:42,280 you can see all the different methods available to you 17 00:00:42,280 --> 00:00:44,470 for the list type in Python, 18 00:00:44,470 --> 00:00:45,920 one of which is sort. 19 00:00:45,920 --> 00:00:48,390 So lets go ahead and invoke sort. 20 00:00:48,390 --> 00:00:51,880 And that's an in-place sort, so now if I look at numbers 21 00:00:51,880 --> 00:00:53,640 after the operation, 22 00:00:53,640 --> 00:00:55,270 you can see that indeed 23 00:00:55,270 --> 00:00:58,270 it modified the list and now the list is in 24 00:00:58,270 --> 00:01:00,580 ascending order by default. 25 00:01:00,580 --> 00:01:02,750 Now let me redefine that list once again, 26 00:01:02,750 --> 00:01:06,447 because you can also sort in descending order by default. 27 00:01:06,447 --> 00:01:09,860 Not by default, but by passing an argument 28 00:01:09,860 --> 00:01:11,590 to the sort method. 29 00:01:11,590 --> 00:01:15,120 So if we say reverse=true, that says 30 00:01:15,120 --> 00:01:17,960 I would like to sort in descending order. 31 00:01:17,960 --> 00:01:21,200 So now if I take a look at the values in numbers, 32 00:01:21,200 --> 00:01:23,510 I can see them top to bottom. 33 00:01:23,510 --> 00:01:25,200 Ten down to one. 34 00:01:25,200 --> 00:01:29,180 So that's in-place sorting with the sort method 35 00:01:29,180 --> 00:01:30,190 of a list. 36 00:01:30,190 --> 00:01:34,120 Now, if you don't want to modify the list's contents 37 00:01:34,120 --> 00:01:38,240 or if you're working with a sequence that's immutable, 38 00:01:38,240 --> 00:01:41,330 there's also a built-in function for Python 39 00:01:41,330 --> 00:01:42,920 called sorted. 40 00:01:42,920 --> 00:01:47,170 So let's go ahead once again and create that list 41 00:01:47,170 --> 00:01:48,893 with the elements out of order. 42 00:01:50,229 --> 00:01:54,170 And for this one, we'll assign the values 43 00:01:54,170 --> 00:01:57,070 to a variable after the operation that's going 44 00:01:57,070 --> 00:01:59,000 to sort those elements. 45 00:01:59,000 --> 00:02:01,050 So let's say we have a variable called 46 00:02:01,050 --> 00:02:04,220 ascending_numbers and we're going to assign that 47 00:02:04,220 --> 00:02:08,940 sorted, with numbers as an argument. 48 00:02:08,940 --> 00:02:11,260 Now, sorted is the built-in function. 49 00:02:11,260 --> 00:02:15,130 It takes as its argument an iterable object, 50 00:02:15,130 --> 00:02:18,290 some sort of sequence I can iterate over. 51 00:02:18,290 --> 00:02:23,290 And it gives back a list of values 52 00:02:23,700 --> 00:02:26,370 in ascending order by default. 53 00:02:26,370 --> 00:02:30,140 So if I now go and display the contents 54 00:02:30,140 --> 00:02:31,210 of ascending_numbers, 55 00:02:31,210 --> 00:02:34,410 we can see that indeed, this new variable 56 00:02:34,410 --> 00:02:37,400 contains the values in ascending order, 57 00:02:37,400 --> 00:02:39,980 but it did not modify numbers. 58 00:02:39,980 --> 00:02:42,280 And to prove that, we can evaluate numbers 59 00:02:42,280 --> 00:02:45,620 and see that indeed, it still contains 60 00:02:45,620 --> 00:02:48,180 the original values that we defined 61 00:02:48,180 --> 00:02:50,120 back in snippet seven. 62 00:02:50,120 --> 00:02:52,380 So that's sorted with a list, 63 00:02:52,380 --> 00:02:55,770 but I also mentioned the fact that you can use sorted 64 00:02:55,770 --> 00:02:58,460 with immutable types as well. 65 00:02:58,460 --> 00:03:01,250 So let's go and create a string called letters. 66 00:03:01,250 --> 00:03:03,740 We just have a bunch of random letters here 67 00:03:03,740 --> 00:03:06,970 and they are clearly not in alphabetical order. 68 00:03:06,970 --> 00:03:08,570 So let's define that. 69 00:03:08,570 --> 00:03:13,500 And we can go ahead and use the sorted method, 70 00:03:13,500 --> 00:03:16,527 or function rather, with letters to get the 71 00:03:16,527 --> 00:03:20,360 characters in this case in alphabetical order. 72 00:03:20,360 --> 00:03:22,943 So let's call this ascending_letters. 73 00:03:25,530 --> 00:03:27,730 And I'm going to assign the result to a variable 74 00:03:27,730 --> 00:03:30,390 for a moment, just to prove to you that the original 75 00:03:30,390 --> 00:03:33,210 letters string is left unmodified. 76 00:03:33,210 --> 00:03:36,640 Now, of course a string is imutable in the first place, 77 00:03:36,640 --> 00:03:39,120 so that should still be the case. 78 00:03:39,120 --> 00:03:43,707 So let's go ahead and sort the letters string. 79 00:03:44,690 --> 00:03:48,013 And now if I evaluate ascending_letters, 80 00:03:49,090 --> 00:03:51,870 you can see that it gives me back a list, 81 00:03:51,870 --> 00:03:53,090 not a string. 82 00:03:53,090 --> 00:03:57,350 A list of the individual letters in alphabetical order. 83 00:03:57,350 --> 00:03:59,940 And actually it's not truly alphabetical order, 84 00:03:59,940 --> 00:04:02,520 it's in lexicographical order, 85 00:04:02,520 --> 00:04:05,360 where it's ordering those characters based on 86 00:04:05,360 --> 00:04:10,230 their underlying numeric values in the character set 87 00:04:10,230 --> 00:04:11,260 used underneath the hood, 88 00:04:11,260 --> 00:04:13,870 which is the Unicode character set. 89 00:04:13,870 --> 00:04:17,230 So here you see that we can in fact sort 90 00:04:17,230 --> 00:04:19,410 the contents of the string letters, 91 00:04:19,410 --> 00:04:21,560 but we get that back as a list 92 00:04:21,560 --> 00:04:23,960 containing the individual letters. 93 00:04:23,960 --> 00:04:28,060 And just to show you that letters remains unchanged, 94 00:04:28,060 --> 00:04:31,010 there is the original letters string. 95 00:04:31,010 --> 00:04:33,610 Now, you can also use this with tuples. 96 00:04:33,610 --> 00:04:36,290 So let's say I have the following tuple, 97 00:04:36,290 --> 00:04:39,790 which is just the collection of strings red, orange, 98 00:04:39,790 --> 00:04:41,240 yellow, green, blue. 99 00:04:41,240 --> 00:04:44,950 And let's create a new variable once again. 100 00:04:44,950 --> 00:04:47,547 So we'll call this one ascending_colors 101 00:04:48,510 --> 00:04:50,113 and let's sort that. 102 00:04:52,920 --> 00:04:57,350 So hopefully now when we evaluate ascending_colors, 103 00:04:58,690 --> 00:05:01,290 we will see those, in this case what appears 104 00:05:01,290 --> 00:05:02,950 to be alphabetic order, 105 00:05:02,950 --> 00:05:06,080 but again, it's really lexicographical order. 106 00:05:06,080 --> 00:05:09,440 So if I had uppercase and lowercase letters 107 00:05:09,440 --> 00:05:11,160 in some of these strings, 108 00:05:11,160 --> 00:05:13,410 they may not appear in the same order 109 00:05:13,410 --> 00:05:15,050 that you're seeing right now. 110 00:05:15,050 --> 00:05:18,180 And separately, notice that it gives me back a list, 111 00:05:18,180 --> 00:05:20,480 even though I started with a tuple. 112 00:05:20,480 --> 00:05:23,970 And finally, notice that if I display colors, 113 00:05:23,970 --> 00:05:27,230 it is still a tuple with the strings in their 114 00:05:27,230 --> 00:05:28,760 original order. 115 00:05:28,760 --> 00:05:30,340 So as you saw here, 116 00:05:30,340 --> 00:05:34,600 we were able to sort lists in-place with the sort method 117 00:05:34,600 --> 00:05:39,450 and we were able to sort lists, string contents, or tuples, 118 00:05:39,450 --> 00:05:41,980 with the sorted built-in function. 119 00:05:41,980 --> 00:05:44,310 The one thing I didn't demonstrate here 120 00:05:44,310 --> 00:05:48,520 is the fact that you can also sort in descending order 121 00:05:48,520 --> 00:05:50,070 with the sorted method. 122 00:05:50,070 --> 00:05:53,360 So for example, if I say sorted, 123 00:05:53,360 --> 00:05:56,280 and let's say we want to sort these color strings 124 00:05:56,280 --> 00:05:57,780 in descending order. 125 00:05:57,780 --> 00:06:00,210 We can give a second argument that says 126 00:06:00,210 --> 00:06:03,730 reverse=True, just like we did with the sort method 127 00:06:03,730 --> 00:06:05,190 built in to a list, 128 00:06:05,190 --> 00:06:08,826 and we get back a list containing those strings now 129 00:06:08,826 --> 00:06:13,100 in descending order from highest to lowest. 130 00:06:13,100 --> 00:06:15,820 And again, because I'm using all lowercase letters, 131 00:06:15,820 --> 00:06:18,730 they're in alphabetical order in descending order 132 00:06:18,730 --> 00:06:19,763 in this case.