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