1 00:00:01,190 --> 00:00:01,609 All right. 2 00:00:01,609 --> 00:00:04,939 Let's learn about some actions that we can take on lists. 3 00:00:04,939 --> 00:00:09,500 And this is very exciting because you can perform a lot of actions on lists. 4 00:00:09,890 --> 00:00:13,370 Now, we've learned about built in functions, right? 5 00:00:13,490 --> 00:00:16,640 Functions that come with Python. 6 00:00:17,530 --> 00:00:24,490 And we've actually seen this one before when we talked about strengths, which is the length function. 7 00:00:24,700 --> 00:00:29,500 And here we can have it calculate the length of a list. 8 00:00:29,500 --> 00:00:35,170 So, for example, if we had a basket here that had one, two, three, four, five. 9 00:00:36,330 --> 00:00:37,980 Well, this basket. 10 00:00:37,980 --> 00:00:43,350 If I calculate the length of the basket, let's do print here so we can see the result. 11 00:00:45,050 --> 00:00:46,190 And I click Run. 12 00:00:47,260 --> 00:00:48,010 I get. 13 00:00:50,920 --> 00:00:58,060 The length of the basket is five because while there's five items, remember a length is the actual 14 00:00:58,060 --> 00:00:58,380 length. 15 00:00:58,390 --> 00:01:00,040 It doesn't start counting from zero. 16 00:01:00,040 --> 00:01:02,980 It's going to do human length, which is five. 17 00:01:03,660 --> 00:01:08,040 But lists get really powerful when it comes to methods. 18 00:01:08,250 --> 00:01:14,580 So instead of a built in function, remember, a method is a nice action that it's owned by something 19 00:01:14,580 --> 00:01:16,980 and it's specific, let's say, to a data type. 20 00:01:17,610 --> 00:01:26,400 So if we go to list methods, you'll see that Python has a few list methods that we can use. 21 00:01:28,320 --> 00:01:36,870 And the way we use these methods is remember, we just add a dot after a list. 22 00:01:37,470 --> 00:01:39,180 So let's have a look at some of them. 23 00:01:40,080 --> 00:01:44,340 First, I'm going to start off with the adding ones. 24 00:01:44,760 --> 00:01:50,580 So let's say in this basket, we want to add something to it to the end of the list. 25 00:01:51,000 --> 00:01:55,110 Well, we can use the append and remember with a method. 26 00:01:55,140 --> 00:02:00,750 As soon as you write the dot, I'll tell you what we can use, which is very, very useful when you 27 00:02:00,750 --> 00:02:01,650 have an editor. 28 00:02:01,710 --> 00:02:03,570 So the first one is a pen. 29 00:02:04,110 --> 00:02:09,840 And if I hover over this, well, it just tells me a pen is an object to end. 30 00:02:10,110 --> 00:02:12,120 And in Python, an object is. 31 00:02:12,120 --> 00:02:15,120 Well, everything in Python is an object. 32 00:02:15,450 --> 00:02:16,740 A number is an object. 33 00:02:16,740 --> 00:02:18,300 A list is an object. 34 00:02:19,260 --> 00:02:21,630 So just think of it as an item for now. 35 00:02:21,810 --> 00:02:25,770 So if I want to append, let's say 100 to the end of the list. 36 00:02:26,040 --> 00:02:34,200 And you know what, let's add a new list here, and this new list will do the append for us. 37 00:02:34,680 --> 00:02:38,280 If I print the new list and I click Run. 38 00:02:39,070 --> 00:02:40,630 I get none. 39 00:02:41,870 --> 00:02:42,290 Hmm. 40 00:02:43,010 --> 00:02:43,880 That's weird. 41 00:02:43,970 --> 00:02:45,800 What if I print? 42 00:02:47,380 --> 00:02:48,400 Basket here. 43 00:02:49,690 --> 00:02:50,920 If I click Run. 44 00:02:52,940 --> 00:02:53,690 All right. 45 00:02:54,080 --> 00:02:55,880 So it looks like. 46 00:02:56,840 --> 00:03:05,900 The basket was appended to we added 100 but a new list when we assigned it. 47 00:03:06,020 --> 00:03:08,390 The basket append. 48 00:03:08,390 --> 00:03:17,300 That new list is completely none and that is because append changes the list in place. 49 00:03:17,570 --> 00:03:19,370 What does in-place mean? 50 00:03:19,790 --> 00:03:22,670 It means that it doesn't produce a value. 51 00:03:22,700 --> 00:03:28,880 All it does is saying, Hey, I'm just going to append 100 to this basket that you gave me, but I don't 52 00:03:28,880 --> 00:03:29,480 really care. 53 00:03:29,480 --> 00:03:31,150 I'm not producing a result. 54 00:03:31,160 --> 00:03:33,260 I'm just changing this for you. 55 00:03:34,110 --> 00:03:35,730 I know it's a little confusing. 56 00:03:36,210 --> 00:03:44,190 So in order for our new list to have that 100 at the end, we have to do something like this. 57 00:03:45,460 --> 00:03:46,480 If I click Run. 58 00:03:48,120 --> 00:03:48,930 There we go. 59 00:03:49,080 --> 00:03:50,670 After we've appended. 60 00:03:51,460 --> 00:03:58,270 To the basket, then we can assign so that new list points to basket, which points to this list that 61 00:03:58,270 --> 00:03:59,140 was modified. 62 00:03:59,860 --> 00:04:00,520 All right. 63 00:04:00,730 --> 00:04:01,930 What else is there? 64 00:04:02,610 --> 00:04:05,520 Well, there's also something called insert. 65 00:04:05,880 --> 00:04:13,260 And you see over here that insert gives an index and an object so we can insert something not at the 66 00:04:13,260 --> 00:04:13,980 end of the list. 67 00:04:13,980 --> 00:04:18,060 I mean, we could, but we can also insert it anywhere we want in an index. 68 00:04:18,060 --> 00:04:22,050 So for example, in this case, if I do insert a 100. 69 00:04:23,830 --> 00:04:27,010 At zero one, two, three, four. 70 00:04:27,040 --> 00:04:30,130 So let's do index of four and I click run. 71 00:04:31,030 --> 00:04:34,120 I've added 100 to the index of four. 72 00:04:34,510 --> 00:04:37,570 If I do index of five and I click Run. 73 00:04:38,390 --> 00:04:41,390 I've added 100 at the end of the list. 74 00:04:41,960 --> 00:04:47,460 Now, let's try and copy this and see if we can just add it into here. 75 00:04:47,480 --> 00:04:48,650 If I click Run. 76 00:04:51,040 --> 00:04:51,580 Again. 77 00:04:51,580 --> 00:04:52,480 Same thing. 78 00:04:52,570 --> 00:04:55,930 Insert modifies the list in place. 79 00:04:56,200 --> 00:04:58,630 It doesn't create a new copy of the list. 80 00:05:00,820 --> 00:05:01,180 All right. 81 00:05:01,180 --> 00:05:03,130 And if we add this. 82 00:05:04,420 --> 00:05:05,440 Like this. 83 00:05:06,180 --> 00:05:07,020 Well. 84 00:05:07,860 --> 00:05:13,060 Once again insert modifies the list in place that is. 85 00:05:13,080 --> 00:05:16,950 It doesn't really output a new list. 86 00:05:16,950 --> 00:05:19,650 It just modifies whatever is existing in memory. 87 00:05:20,850 --> 00:05:24,720 Finally, there is another method called extend. 88 00:05:24,720 --> 00:05:32,160 An extend instead of an actual item or object takes what we call an iterable, which we're going to 89 00:05:32,160 --> 00:05:33,330 get into later on. 90 00:05:33,330 --> 00:05:39,000 But it's something that you can loop over, you can iterate over, which is a list. 91 00:05:39,000 --> 00:05:44,730 So we just give it another list like let's say 100 or 101. 92 00:05:46,120 --> 00:05:50,950 So if I run this once again, it doesn't output a new list. 93 00:05:50,950 --> 00:05:54,550 It just modifies the list in place and adds on. 94 00:05:55,810 --> 00:05:57,490 Or extends our list. 95 00:05:58,150 --> 00:06:01,180 And we can also just give it one item. 96 00:06:02,760 --> 00:06:07,080 All right, let's keep going with the removing methods. 97 00:06:07,990 --> 00:06:14,170 Now with the removing, we once again have a few fun things that we can do. 98 00:06:14,740 --> 00:06:20,980 So let's continue with this basket that we've been using that now has 100 included into it. 99 00:06:21,550 --> 00:06:25,750 And the way we can remove things is, well, there's a few ways. 100 00:06:25,810 --> 00:06:30,220 First one is basket dot pop. 101 00:06:31,630 --> 00:06:33,250 And Basketball Pop. 102 00:06:34,160 --> 00:06:43,010 If I go like this and let's print this basket, I'm going to comment out the new list for now since 103 00:06:43,010 --> 00:06:44,060 we're not using it. 104 00:06:44,360 --> 00:06:48,710 And then we're going to say basket, dot, pop and then print. 105 00:06:50,850 --> 00:06:51,450 Basket. 106 00:06:52,270 --> 00:06:53,410 Let's run this. 107 00:06:54,350 --> 00:06:58,820 And Pop Pop's off whatever is at the end of the list. 108 00:06:59,300 --> 00:07:03,560 In our case at the end of the list, because we've extended the basket to 100. 109 00:07:03,920 --> 00:07:06,180 That 100 gets removed. 110 00:07:06,200 --> 00:07:09,140 If I do basket dot pop again. 111 00:07:10,440 --> 00:07:10,660 Oop. 112 00:07:10,710 --> 00:07:11,490 Not here. 113 00:07:11,670 --> 00:07:12,570 Down here. 114 00:07:12,870 --> 00:07:13,890 And I run. 115 00:07:15,350 --> 00:07:18,380 You see that both 105 get removed? 116 00:07:19,170 --> 00:07:21,780 What if I do pop zero? 117 00:07:22,540 --> 00:07:23,890 Five Click Run here. 118 00:07:26,660 --> 00:07:29,030 It removes the item. 119 00:07:29,850 --> 00:07:31,170 In the index. 120 00:07:31,380 --> 00:07:37,500 So here, Pop zero is going to remove whatever is that index of zero, which is one. 121 00:07:38,100 --> 00:07:41,250 Now there's also dot remove. 122 00:07:42,390 --> 00:07:48,250 Remove again is we give it a value that we want to remove. 123 00:07:48,270 --> 00:07:51,720 So in our case, let's say we want to remove number four. 124 00:07:51,990 --> 00:07:53,610 Well, if we run this. 125 00:07:55,670 --> 00:07:58,160 It's going to remove four for us. 126 00:07:58,790 --> 00:08:04,430 So remove we give it the value that we want to remove with pop. 127 00:08:04,430 --> 00:08:07,130 We give it the index that we want to remove. 128 00:08:07,700 --> 00:08:14,300 And just to see if this is working, let's add a new list here. 129 00:08:15,270 --> 00:08:19,830 And we'll say new list equals basket remove. 130 00:08:20,730 --> 00:08:24,120 Add a new list here, see if that gets modified. 131 00:08:25,300 --> 00:08:27,240 No, it does not get modified. 132 00:08:27,250 --> 00:08:30,310 That means remove is working in place. 133 00:08:30,310 --> 00:08:32,440 It doesn't return a value. 134 00:08:33,210 --> 00:08:36,419 It just simply changes whatever list you give it. 135 00:08:36,780 --> 00:08:38,490 What if we do, Pop? 136 00:08:39,260 --> 00:08:42,350 If I run this, I get five. 137 00:08:42,679 --> 00:08:43,070 Hmm. 138 00:08:43,669 --> 00:08:45,380 Why is that? 139 00:08:45,830 --> 00:08:48,680 And this is something that you just have to get used to. 140 00:08:48,710 --> 00:08:50,750 Different methods do different things. 141 00:08:50,840 --> 00:08:52,310 For example, pop. 142 00:08:52,430 --> 00:08:59,180 The way it works is that pop returns whatever you have just removed. 143 00:08:59,210 --> 00:09:07,100 In our case, when we did four, that is index of zero one, two, three, four it return the number 144 00:09:07,100 --> 00:09:08,390 five for me. 145 00:09:09,720 --> 00:09:14,580 Even though it removed it from the basket, it still returned something. 146 00:09:14,730 --> 00:09:18,600 And the reason for others is that we got none. 147 00:09:18,630 --> 00:09:22,530 That is when a method doesn't return anything, it returns none. 148 00:09:22,560 --> 00:09:24,510 A topic we're going to cover shortly. 149 00:09:25,690 --> 00:09:30,160 So you have to be careful and understand what each method returns. 150 00:09:30,160 --> 00:09:31,840 If I go over extend. 151 00:09:32,350 --> 00:09:37,150 I see here that this little arrow shows me that none is returned. 152 00:09:38,260 --> 00:09:42,760 That means it's not going to produce anything with this method. 153 00:09:42,760 --> 00:09:46,360 It's only going to change or extend a list that it was given. 154 00:09:47,560 --> 00:09:51,340 Don't worry if this is confusing, that's something that you're going to get used to as we get more 155 00:09:51,340 --> 00:09:52,330 and more practice. 156 00:09:52,630 --> 00:09:58,630 Now, the last removing method I want to show you is clear and clear. 157 00:09:58,960 --> 00:10:02,080 As you might guess, if I click run here. 158 00:10:03,360 --> 00:10:05,240 Well, this is none. 159 00:10:05,250 --> 00:10:09,870 But if I go to Basket and I click Run. 160 00:10:11,660 --> 00:10:13,640 The basket is empty. 161 00:10:13,730 --> 00:10:17,090 Clear removes whatever is in the list. 162 00:10:17,090 --> 00:10:18,620 Completely clears it. 163 00:10:19,390 --> 00:10:21,100 Just as a name suggests. 164 00:10:22,190 --> 00:10:22,730 All right. 165 00:10:22,730 --> 00:10:24,950 That was a lot, but there's still a few more. 166 00:10:24,950 --> 00:10:27,140 So let's take a break and I'll see you in the next video. 167 00:10:27,410 --> 00:10:27,980 Bye bye.