1 00:00:01,050 --> 00:00:02,190 -: Welcome back. 2 00:00:02,190 --> 00:00:04,080 Let's talk about some useful tricks 3 00:00:04,080 --> 00:00:08,280 with lists that you'll see a lot in Python code. 4 00:00:08,280 --> 00:00:12,120 Well, one we have already seen, it's this idea of length. 5 00:00:12,120 --> 00:00:14,130 You're gonna be using this a lot. 6 00:00:14,130 --> 00:00:17,110 That is to figure out the length of a list 7 00:00:18,030 --> 00:00:20,910 which in our case is now seven. 8 00:00:20,910 --> 00:00:25,890 Another really useful trick is to reverse a list. 9 00:00:25,890 --> 00:00:29,850 Now, I've already showed you how to reverse a list 10 00:00:29,850 --> 00:00:33,660 but sometimes you can do that with list slicing. 11 00:00:33,660 --> 00:00:36,060 Again, something that we've seen in string slicing 12 00:00:36,060 --> 00:00:39,310 so I can actually do basket 13 00:00:40,200 --> 00:00:44,460 and then semicolon, semicolon, negative one. 14 00:00:44,460 --> 00:00:48,573 If I click run here, is that what you expected? 15 00:00:50,190 --> 00:00:51,940 Let's go through this line by line. 16 00:00:52,980 --> 00:00:57,980 I've sorted the basket so that it's from A all the way to Z 17 00:00:59,220 --> 00:01:03,840 and then I've reversed this, so it's in Z all the way to A 18 00:01:03,840 --> 00:01:08,790 and then I've reversed the basket again, so that instead 19 00:01:08,790 --> 00:01:13,533 of being in reverse, it's now in order A, B, C, D, E, X. 20 00:01:14,520 --> 00:01:19,520 And this list slicing creates a new list, right? 21 00:01:22,380 --> 00:01:23,650 If I print 22 00:01:25,200 --> 00:01:26,140 basket here 23 00:01:28,140 --> 00:01:32,550 you'll see that the basket is the reversed version 24 00:01:32,550 --> 00:01:35,700 the last action that we've taken on it 25 00:01:35,700 --> 00:01:40,680 versus list slicing that creates a new version. 26 00:01:40,680 --> 00:01:42,960 So this notation, you'll see a lot 27 00:01:42,960 --> 00:01:45,780 with lists when you need to reverse a list 28 00:01:45,780 --> 00:01:50,400 or maybe you need to make a copy of the list like this 29 00:01:50,400 --> 00:01:53,220 or maybe you need a portion of the list. 30 00:01:53,220 --> 00:01:56,040 Another useful thing that you'll see a lot of 31 00:01:56,040 --> 00:01:59,250 is something called range. 32 00:01:59,250 --> 00:02:01,560 Let's comment this out for now. 33 00:02:01,560 --> 00:02:05,070 If I wanted to generate a list really quickly, let's say 34 00:02:05,070 --> 00:02:08,639 from one to a hundred, I can do something like this. 35 00:02:08,639 --> 00:02:13,640 I can say range, and then start, and then stop. 36 00:02:13,920 --> 00:02:16,983 So, let's do a hundred, 37 00:02:18,000 --> 00:02:19,600 and if I print this 38 00:02:23,160 --> 00:02:24,720 and I run, hmm, 39 00:02:24,720 --> 00:02:27,333 I get a range one to a hundred. 40 00:02:28,230 --> 00:02:32,640 But if I wrap this in a list, which is going to 41 00:02:32,640 --> 00:02:34,893 create a new list for us, and I click run, 42 00:02:35,940 --> 00:02:36,773 look at that. 43 00:02:36,773 --> 00:02:40,230 I get a range from one all the way to 99. 44 00:02:40,230 --> 00:02:43,710 Remember, it's the start and then whatever to stop at. 45 00:02:43,710 --> 00:02:46,620 So before it hits a hundred, it's going to stop. 46 00:02:46,620 --> 00:02:51,430 Now, if I remove this and just do one single thing 47 00:02:52,830 --> 00:02:56,400 I get a list starting from zero all the way to 99. 48 00:02:56,400 --> 00:03:01,140 If I want a list that has 101 items, because it starts 49 00:03:01,140 --> 00:03:04,113 at zero, I would just do 101. 50 00:03:05,130 --> 00:03:08,700 And range is something we'll talk about a lot more 51 00:03:08,700 --> 00:03:12,450 especially when we talk about loops in programming. 52 00:03:12,450 --> 00:03:16,530 But this is a great way of you to generate a list really 53 00:03:16,530 --> 00:03:19,650 really quickly, especially a numbered list. 54 00:03:19,650 --> 00:03:22,950 Finally, the last common thing that you'll see a lot 55 00:03:22,950 --> 00:03:25,803 is this idea of dot join. 56 00:03:26,640 --> 00:03:28,590 What is dot join? 57 00:03:28,590 --> 00:03:31,770 Join is actually something that works on strings. 58 00:03:31,770 --> 00:03:36,750 It's a string method, but it's often used this way. 59 00:03:36,750 --> 00:03:41,750 We have an empty string and this empty string 60 00:03:42,480 --> 00:03:47,480 let's say sentence, and then we can do sentence dot, join 61 00:03:50,130 --> 00:03:53,370 and join takes what we call an iterable, 62 00:03:53,370 --> 00:03:56,190 which is a list for now. 63 00:03:56,190 --> 00:04:00,067 And in here we can have a sentence like, 64 00:04:00,067 --> 00:04:05,067 "Hi, my name is Jojo." 65 00:04:11,400 --> 00:04:14,440 And now if I print the sentence 66 00:04:15,390 --> 00:04:19,680 and I click run here, hmm, I don't get anything. 67 00:04:19,680 --> 00:04:21,390 Let's figure out why. 68 00:04:21,390 --> 00:04:25,083 What if I type in an exclamation mark here and I click run? 69 00:04:26,820 --> 00:04:29,580 All right, it's printing out sentence, but it looks 70 00:04:29,580 --> 00:04:33,240 like dot join creates a new item for us. 71 00:04:33,240 --> 00:04:36,543 So let's just assign that to new sentence. 72 00:04:41,100 --> 00:04:45,310 Now, if I do new sentence and print that out 73 00:04:48,330 --> 00:04:52,233 we see that I get, Hi, my name is Jojo. 74 00:04:53,250 --> 00:04:57,690 Hmm. So this is a little funky, right? 75 00:04:57,690 --> 00:05:02,690 We have these exclamation marks between our items 76 00:05:02,970 --> 00:05:06,246 and join joins these little iterable items 77 00:05:06,246 --> 00:05:10,170 these lists by whatever is in front of it. 78 00:05:10,170 --> 00:05:12,000 So if we actually change this 79 00:05:12,000 --> 00:05:17,000 to an empty string, click run, we now get this. 80 00:05:17,280 --> 00:05:20,133 If I add a space in between and click run, 81 00:05:21,060 --> 00:05:23,283 we get, Hi, my name is Jojo. 82 00:05:24,840 --> 00:05:27,450 And sometimes you'll notice this pattern a lot 83 00:05:27,450 --> 00:05:29,010 especially if you wanna join 84 00:05:29,010 --> 00:05:33,240 and create a new string out of a list of items. 85 00:05:33,240 --> 00:05:34,410 Now, a shorthand way 86 00:05:34,410 --> 00:05:39,000 of doing this is to actually not even bother with this 87 00:05:39,000 --> 00:05:42,840 and just doing an empty string dot join 88 00:05:42,840 --> 00:05:45,933 and then joining all the list items. 89 00:05:47,730 --> 00:05:48,900 There we go. 90 00:05:48,900 --> 00:05:52,080 So all we are doing is combining lists into a string 91 00:05:52,080 --> 00:05:55,110 and this is a common pattern that you'll see a lot of. 92 00:05:55,110 --> 00:05:56,123 I'll see in the next one. 93 00:05:56,123 --> 00:05:56,973 Bye-bye.