1 00:00:00,317 --> 00:00:01,150 Instructor: All right. 2 00:00:01,150 --> 00:00:02,940 It feels like it's never going to end. 3 00:00:02,940 --> 00:00:05,610 We have so many methods with lists but, 4 00:00:05,610 --> 00:00:08,189 like I said lists have a lot of methods 5 00:00:08,189 --> 00:00:09,930 because you can do a lot with them. 6 00:00:09,930 --> 00:00:11,310 It seems like a lot right now, 7 00:00:11,310 --> 00:00:13,770 but trust me, these are going to become second nature. 8 00:00:13,770 --> 00:00:15,000 So, hang in there. 9 00:00:15,000 --> 00:00:17,220 I have a few more to show you. 10 00:00:17,220 --> 00:00:20,823 The next one is called sort. 11 00:00:21,810 --> 00:00:23,850 Guess what sort is going to do? 12 00:00:23,850 --> 00:00:25,173 If I click run, 13 00:00:27,330 --> 00:00:29,280 I get none. 14 00:00:29,280 --> 00:00:33,690 Hmm, but remember sort, sorts the list, 15 00:00:33,690 --> 00:00:35,760 as you can imagine, in place. 16 00:00:35,760 --> 00:00:37,590 That means it modifies the list. 17 00:00:37,590 --> 00:00:40,320 So if I remove this up to here 18 00:00:40,320 --> 00:00:44,280 and then do sort or do basket, 19 00:00:44,280 --> 00:00:45,573 and I click run, 20 00:00:47,370 --> 00:00:48,203 look at that. 21 00:00:48,203 --> 00:00:53,160 I get a sorted list where D was moved in front of E. 22 00:00:53,160 --> 00:00:57,010 If I add, let's say, X at the beginning here 23 00:00:58,680 --> 00:01:02,160 and I click run, it's going to move the X 24 00:01:02,160 --> 00:01:03,330 all the way to the end. 25 00:01:03,330 --> 00:01:06,480 It's going to sort the list in place for me. 26 00:01:06,480 --> 00:01:09,300 Again, if I hover over it, it sees that it's 27 00:01:09,300 --> 00:01:11,280 going to not return anything, 28 00:01:11,280 --> 00:01:13,650 which is why our print initially didn't work. 29 00:01:13,650 --> 00:01:17,160 We had to print our basket, not whatever this produces, 30 00:01:17,160 --> 00:01:18,860 which it doesn't produce anything. 31 00:01:19,950 --> 00:01:21,660 Hmm, but what about this one? 32 00:01:21,660 --> 00:01:23,490 If we go to built-in functions, 33 00:01:23,490 --> 00:01:26,430 I have something called sorted, 34 00:01:26,430 --> 00:01:28,320 and if I do sorted, 35 00:01:28,320 --> 00:01:31,860 and, because it's a function, not a method, 36 00:01:31,860 --> 00:01:35,880 I can do sorted dot or basket. 37 00:01:35,880 --> 00:01:38,220 So, around... 38 00:01:38,220 --> 00:01:40,260 the bracket is just like print. 39 00:01:40,260 --> 00:01:42,420 If I run this, 40 00:01:42,420 --> 00:01:44,640 right, nothing has changed. 41 00:01:44,640 --> 00:01:49,383 But what if I remove this and put it in here? 42 00:01:50,220 --> 00:01:55,140 If I click run, I get A, B, C, D, D, E, X. 43 00:01:55,140 --> 00:01:57,273 Hmm, what if I comment this out, 44 00:01:58,800 --> 00:02:00,600 so that we're not sorting it anymore? 45 00:02:00,600 --> 00:02:01,500 We just have the basket, 46 00:02:01,500 --> 00:02:04,620 and then we run this sorted function. 47 00:02:04,620 --> 00:02:05,453 If I run, 48 00:02:07,666 --> 00:02:08,515 did you see that? 49 00:02:09,750 --> 00:02:11,610 Sorted, I know it's confusing. 50 00:02:11,610 --> 00:02:13,140 Why do they even have two? 51 00:02:13,140 --> 00:02:16,593 But, sorted produces a new array. 52 00:02:18,540 --> 00:02:23,373 So if I do print basket here and I click run, 53 00:02:25,650 --> 00:02:29,013 the basket hasn't been modified. 54 00:02:30,630 --> 00:02:32,730 The basket still remains the same. 55 00:02:32,730 --> 00:02:35,010 It's this one over here that's not sorted. 56 00:02:35,010 --> 00:02:39,000 Sorted, on the other hand, doesn't modify the basket 57 00:02:39,000 --> 00:02:39,833 in place. 58 00:02:39,833 --> 00:02:44,520 Instead, it creates a new copy of the basket and sorts it. 59 00:02:44,520 --> 00:02:47,040 It's almost as if we're doing this. 60 00:02:47,040 --> 00:02:50,760 We're saying new basket 61 00:02:50,760 --> 00:02:54,870 equals basket, and then we're going to copy that basket. 62 00:02:54,870 --> 00:02:55,770 Remember? 63 00:02:55,770 --> 00:02:57,660 List slicing. 64 00:02:57,660 --> 00:03:01,500 And then, we're performing new_basket.sort() 65 00:03:01,500 --> 00:03:03,510 on this new list. 66 00:03:03,510 --> 00:03:06,750 So that, if I do new basket 67 00:03:06,750 --> 00:03:11,750 and I click run, we get the same thing. 68 00:03:11,820 --> 00:03:13,410 So you have to be careful 69 00:03:13,410 --> 00:03:17,160 of certain actions that perform an action 70 00:03:17,160 --> 00:03:20,853 in place or create a completely new list to begin with. 71 00:03:22,740 --> 00:03:25,350 By the way, there's also, instead of this 72 00:03:25,350 --> 00:03:29,010 a dot copy method that we can use. 73 00:03:29,010 --> 00:03:32,880 And if I run this again, we get the same thing. 74 00:03:32,880 --> 00:03:35,220 Copy, just as the name suggests, 75 00:03:35,220 --> 00:03:38,733 copy the list for us and returns for us a new list. 76 00:03:40,110 --> 00:03:45,110 All right, one last one, and that is basket.reverse. 77 00:03:48,450 --> 00:03:50,750 What do you think this does based on the name? 78 00:03:52,050 --> 00:03:57,040 Well, if I check out the basket here and click run 79 00:03:58,950 --> 00:04:00,250 is that what you expected? 80 00:04:01,740 --> 00:04:04,660 It reverses the basket 81 00:04:05,850 --> 00:04:07,470 in place. 82 00:04:07,470 --> 00:04:10,590 But notice that it's not like sort, it's not sorting. 83 00:04:10,590 --> 00:04:15,590 It's simply switching all the indexes into opposite sides. 84 00:04:16,230 --> 00:04:18,570 So D is now index of zero. 85 00:04:18,570 --> 00:04:22,350 E is now index of one, index of two, three, 86 00:04:22,350 --> 00:04:23,730 so on and so forth. 87 00:04:23,730 --> 00:04:26,940 So you can see here that it doesn't actually sort it. 88 00:04:26,940 --> 00:04:31,940 Instead, while we might wanna do is sort first, then reverse 89 00:04:32,790 --> 00:04:37,790 and if we click run, we have a sorted reversed basket. 90 00:04:40,380 --> 00:04:41,640 This is a little tricky one 91 00:04:41,640 --> 00:04:44,520 that some interviewers might ask. 92 00:04:44,520 --> 00:04:46,464 All right, good job getting this far. 93 00:04:46,464 --> 00:04:49,260 We're almost done with lists. 94 00:04:49,260 --> 00:04:50,711 So I'll see you in the next video. 95 00:04:50,711 --> 00:04:51,544 Bye-bye.