1 00:00:01,040 --> 00:00:01,590 Welcome back. 2 00:00:02,180 --> 00:00:07,700 Let's talk about some useful tricks with lists that you'll see a lot in Python code. 3 00:00:08,240 --> 00:00:10,460 Well, one we have already seen. 4 00:00:10,460 --> 00:00:12,110 It's this idea of lenth. 5 00:00:12,140 --> 00:00:19,160 You're going to be using this a lot, that is to figure out the length of a list, which in our case 6 00:00:19,160 --> 00:00:20,170 is now seven. 7 00:00:20,870 --> 00:00:25,710 Another really useful trick is to reverse a list. 8 00:00:25,880 --> 00:00:33,650 Now, I've already showed you how to reverse a list, but sometimes you can do that with list slicing 9 00:00:33,680 --> 00:00:35,710 against something that we've seen in string slicing. 10 00:00:36,020 --> 00:00:39,290 So I can actually do BASKETT. 11 00:00:40,190 --> 00:00:45,920 And then semicolon, semicolon, negative one, if I click, run here. 12 00:00:47,190 --> 00:00:48,450 Is that what you expected? 13 00:00:50,240 --> 00:00:51,680 Let's go through this line by line. 14 00:00:52,950 --> 00:01:01,710 I've sorted the basket so that it's from a all the way to Z and then I've reversed this, so it's in 15 00:01:01,950 --> 00:01:10,500 Z all the way to A and then I've reversed the basket again so that instead of being in reverse, it's 16 00:01:10,500 --> 00:01:11,300 now in order. 17 00:01:11,310 --> 00:01:13,350 ABCDE e x. 18 00:01:14,480 --> 00:01:26,060 And this list slicing creates a new list, right, if I print basket here. 19 00:01:28,140 --> 00:01:36,780 You'll see that the basket is the reversed version, the last action that we've taken on it versus list 20 00:01:36,780 --> 00:01:39,870 slicing, that creates a new version. 21 00:01:40,680 --> 00:01:45,750 So this notation, you'll see a lot with lists when you need to reverse the list. 22 00:01:45,780 --> 00:01:52,470 Or maybe you need to make a copy of the list like this, or maybe you need a portion of the list. 23 00:01:53,190 --> 00:01:58,710 Another useful thing that you'll see a lot of is something called range. 24 00:01:59,220 --> 00:02:01,020 Let's come at this out for now. 25 00:02:01,530 --> 00:02:07,590 If I wanted to generate a list really quickly, let's say from one to one hundred, I can do something 26 00:02:07,590 --> 00:02:08,060 like this. 27 00:02:08,610 --> 00:02:13,590 I can say range and then start and then stop. 28 00:02:13,890 --> 00:02:16,860 So let's do one hundred. 29 00:02:17,960 --> 00:02:19,490 And if I print this. 30 00:02:23,130 --> 00:02:27,240 And I run, hmm, I get a range one to 100. 31 00:02:28,220 --> 00:02:30,770 But if I wrap this in a list. 32 00:02:31,900 --> 00:02:34,750 Which is going to create a new list for us and I click Run! 33 00:02:35,930 --> 00:02:42,290 Look at that, I get a range from one all the way to ninety nine, remember, it's the start and then 34 00:02:42,290 --> 00:02:43,580 whatever to stop it. 35 00:02:43,700 --> 00:02:46,050 So before it hits one hundred, it's going to stop. 36 00:02:46,610 --> 00:02:51,380 Now, if I remove this and just do one single thing. 37 00:02:52,800 --> 00:03:00,420 I get a list starting from zero all the way to 99, if I want to list that has a hundred and one items 38 00:03:00,420 --> 00:03:03,990 because it starts at zero, I would just do one hundred and one. 39 00:03:05,060 --> 00:03:11,600 And range is something we'll talk about a lot more, especially when we talk about loops in programming, 40 00:03:12,440 --> 00:03:19,250 but this is a great way of you to generate a list really, really quickly, especially a numbered list. 41 00:03:19,640 --> 00:03:25,670 Finally, the last common thing that you'll see a lot is this idea of dot join. 42 00:03:26,630 --> 00:03:27,890 What is dot join. 43 00:03:28,610 --> 00:03:31,790 Join is actually something that works on strings. 44 00:03:31,790 --> 00:03:36,320 It's a string method, but it's often used this way. 45 00:03:36,740 --> 00:03:38,960 We have an empty string. 46 00:03:40,090 --> 00:03:43,450 And this empty string, let's say, sentence. 47 00:03:44,920 --> 00:03:48,220 And then we can do sentence dot join. 48 00:03:50,120 --> 00:03:59,060 And join takes what we call an iterable, which is a list for now, and in here we can have a sentence 49 00:03:59,060 --> 00:04:04,850 like, Hi, my name. 50 00:04:11,350 --> 00:04:14,320 And now if I print sentence. 51 00:04:15,370 --> 00:04:23,140 And I click, run here, hmm, I don't get anything, let's figure out why, what if I type in an exclamation 52 00:04:23,140 --> 00:04:24,940 mark here and I click run? 53 00:04:26,780 --> 00:04:33,140 All right, it's printing out sentence, but it looks like Dudd Join creates a new item for us. 54 00:04:33,230 --> 00:04:36,380 So let's just assign that to new sentence. 55 00:04:41,080 --> 00:04:45,280 Now, if I do a new sentence and print that out. 56 00:04:48,340 --> 00:04:49,070 We see that I. 57 00:04:49,360 --> 00:04:52,090 Hi, my name is Jojo. 58 00:04:54,770 --> 00:04:59,720 So this is a little funky, right, we have these exclamation marks. 59 00:05:01,270 --> 00:05:09,310 Between our items and join Joynes, these little iterable items, these lists by whatever is in front 60 00:05:09,310 --> 00:05:09,530 of it. 61 00:05:10,150 --> 00:05:17,080 So if we actually change this to an empty string click run, we now get this. 62 00:05:17,260 --> 00:05:19,990 If I add a space in between and click run. 63 00:05:21,050 --> 00:05:23,210 We get hi, my name is Jojo. 64 00:05:24,810 --> 00:05:30,300 And sometimes you'll notice this pattern a lot, especially if you want to join and create a new string 65 00:05:30,540 --> 00:05:32,640 out of a list of items. 66 00:05:33,230 --> 00:05:40,050 Now, a shorthand way of doing this is to actually not even bother with this and just doing an empty 67 00:05:40,050 --> 00:05:40,500 string. 68 00:05:41,910 --> 00:05:45,780 Don't join and then joining all the list items. 69 00:05:47,690 --> 00:05:48,270 There we go. 70 00:05:48,860 --> 00:05:53,960 So all we're doing is combining list into a string, and this is a common pattern that you'll see a 71 00:05:53,960 --> 00:05:56,870 lot of I'll see in the next one by.