1 00:00:01,230 --> 00:00:03,480 -: All right, let's learn about some actions 2 00:00:03,480 --> 00:00:04,980 that we can take on lists. 3 00:00:04,980 --> 00:00:06,210 And this is very exciting 4 00:00:06,210 --> 00:00:10,020 because you can perform a lot of actions on lists. 5 00:00:10,020 --> 00:00:13,440 Now, we've learned about built-in functions, right? 6 00:00:13,440 --> 00:00:16,540 Functions that come with Python 7 00:00:17,610 --> 00:00:20,220 and we've actually seen this one before 8 00:00:20,220 --> 00:00:21,990 when we talked about strings, 9 00:00:21,990 --> 00:00:24,840 which is the length function. 10 00:00:24,840 --> 00:00:29,610 And here we can have it calculate the length of a list. 11 00:00:29,610 --> 00:00:31,800 So for example if we had a basket here 12 00:00:31,800 --> 00:00:35,073 that had 1, 2, 3, 4, 5. 13 00:00:36,450 --> 00:00:41,160 Well this basket, if I calculate the length of a basket 14 00:00:41,160 --> 00:00:43,410 let's do print here so we can see the result. 15 00:00:45,120 --> 00:00:46,143 And I click run, 16 00:00:47,310 --> 00:00:48,170 I get... 17 00:00:49,890 --> 00:00:50,970 five. 18 00:00:50,970 --> 00:00:53,190 The length of the basket is five 19 00:00:53,190 --> 00:00:55,980 because well, there's five items. 20 00:00:55,980 --> 00:00:58,380 Remember a length is the actual length. 21 00:00:58,380 --> 00:01:00,090 It doesn't start counting from zero. 22 00:01:00,090 --> 00:01:02,943 It's gonna do human length, which is five. 23 00:01:03,780 --> 00:01:08,400 But lists get really powerful when it comes to methods. 24 00:01:08,400 --> 00:01:10,650 So instead of a built-in function, 25 00:01:10,650 --> 00:01:13,080 remember a method is an action 26 00:01:13,080 --> 00:01:14,610 that's owned by something 27 00:01:14,610 --> 00:01:17,760 and it's specific, let's say, to a data type. 28 00:01:17,760 --> 00:01:21,570 So if we go to list methods, 29 00:01:21,570 --> 00:01:25,470 you'll see that Python has a few list methods 30 00:01:25,470 --> 00:01:26,373 that we can use. 31 00:01:28,410 --> 00:01:32,340 And the way we use these methods 32 00:01:32,340 --> 00:01:34,450 is remember we just add a dot 33 00:01:35,310 --> 00:01:37,620 after a list. 34 00:01:37,620 --> 00:01:39,862 So let's have a look at some of them. 35 00:01:39,862 --> 00:01:44,862 First, I'm going to start off with the adding ones. 36 00:01:44,910 --> 00:01:47,310 So let's say in this basket 37 00:01:47,310 --> 00:01:51,180 we wanna add something to it, to the end of the list. 38 00:01:51,180 --> 00:01:55,140 Well, we can use the append and remember, with a method 39 00:01:55,140 --> 00:01:56,490 as soon as you write the dot 40 00:01:56,490 --> 00:01:58,920 it'll tell you what we can use, 41 00:01:58,920 --> 00:02:01,890 which is very very useful when you have an editor. 42 00:02:01,890 --> 00:02:04,230 So the first one is append. 43 00:02:04,230 --> 00:02:06,600 And if I hover over this, 44 00:02:06,600 --> 00:02:10,229 well it just tells me append an object to end. 45 00:02:10,229 --> 00:02:12,090 And in Python, an object is, 46 00:02:12,090 --> 00:02:15,540 well everything in Python is an object. 47 00:02:15,540 --> 00:02:16,710 A number is an object. 48 00:02:16,710 --> 00:02:18,273 A list is an object. 49 00:02:19,380 --> 00:02:21,990 So just think of it as an item for now. 50 00:02:21,990 --> 00:02:23,250 So if I wanna append 51 00:02:23,250 --> 00:02:26,940 let's say 100 to the end of the list, and you know what? 52 00:02:26,940 --> 00:02:30,420 Let's add a new list here. 53 00:02:30,420 --> 00:02:34,800 And this new list, we'll do the append for us. 54 00:02:34,800 --> 00:02:38,253 If I print the new list and I click run, 55 00:02:39,090 --> 00:02:40,623 I get none. 56 00:02:41,850 --> 00:02:44,130 Hmm, that's weird. 57 00:02:44,130 --> 00:02:45,670 What if I print 58 00:02:47,430 --> 00:02:48,363 basket here? 59 00:02:49,710 --> 00:02:50,913 If I click run? 60 00:02:52,980 --> 00:02:55,750 All right, so it looks like 61 00:02:56,880 --> 00:02:59,700 the basket was appended to, 62 00:02:59,700 --> 00:03:04,560 we added a hundred but a new list, 63 00:03:04,560 --> 00:03:06,150 when we assigned it, 64 00:03:06,150 --> 00:03:11,150 the basket dot append, that new list is completely none. 65 00:03:11,670 --> 00:03:16,230 And that is because append changes the list 66 00:03:16,230 --> 00:03:17,730 in place. 67 00:03:17,730 --> 00:03:19,860 What does in place mean? 68 00:03:19,860 --> 00:03:22,800 It means that it doesn't produce a value. 69 00:03:22,800 --> 00:03:24,540 All it does is saying, Hey 70 00:03:24,540 --> 00:03:26,331 I'm just going to append a hundred 71 00:03:26,331 --> 00:03:29,460 to this basket that you gave me, but I don't really care. 72 00:03:29,460 --> 00:03:31,230 I'm not producing a result. 73 00:03:31,230 --> 00:03:33,183 I'm just changing this for you. 74 00:03:34,170 --> 00:03:36,390 I know it's a little confusing. 75 00:03:36,390 --> 00:03:38,220 So in order for our new list 76 00:03:38,220 --> 00:03:41,400 to have that a hundred at the end, 77 00:03:41,400 --> 00:03:44,193 we have to do something like this. 78 00:03:45,510 --> 00:03:46,473 If I click run, 79 00:03:48,180 --> 00:03:49,170 there we go. 80 00:03:49,170 --> 00:03:53,880 After we've appended to the basket, then we can assign. 81 00:03:53,880 --> 00:03:55,860 So that new list points to basket 82 00:03:55,860 --> 00:03:59,910 which points to this list that was modified. 83 00:03:59,910 --> 00:04:01,863 All right, what else is there? 84 00:04:02,730 --> 00:04:06,000 Well, there's also something called insert. 85 00:04:06,000 --> 00:04:07,980 And you see over here that insert 86 00:04:07,980 --> 00:04:10,410 gives an index and an object. 87 00:04:10,410 --> 00:04:14,010 So we can insert something not at the end of the list. 88 00:04:14,010 --> 00:04:16,050 I mean, we could but we can also insert it 89 00:04:16,050 --> 00:04:18,240 anywhere we want in an index. 90 00:04:18,240 --> 00:04:21,910 So for example, in this case, if I do insert a hundred 91 00:04:23,850 --> 00:04:28,850 at 0 1, 2, 3, 4, so let's do index of four, 92 00:04:29,040 --> 00:04:30,093 and I click run. 93 00:04:31,110 --> 00:04:34,560 I've added a hundred to the index of four. 94 00:04:34,560 --> 00:04:37,450 If I do index of five and I click run 95 00:04:38,490 --> 00:04:42,120 I've added a hundred at the end of the list. 96 00:04:42,120 --> 00:04:44,371 Now let's try and copy this 97 00:04:44,371 --> 00:04:47,610 and see if we can just add it into here. 98 00:04:47,610 --> 00:04:48,663 If I click run, 99 00:04:51,030 --> 00:04:52,770 again, same thing. 100 00:04:52,770 --> 00:04:56,250 Insert modifies the list in place. 101 00:04:56,250 --> 00:04:58,593 It doesn't create a new copy of the list. 102 00:05:00,870 --> 00:05:01,703 All right? 103 00:05:01,703 --> 00:05:05,433 And if we add this, like this, 104 00:05:06,330 --> 00:05:10,560 well once again insert modifies 105 00:05:10,560 --> 00:05:12,360 the list in place. 106 00:05:12,360 --> 00:05:16,950 That is, it doesn't really output a new list. 107 00:05:16,950 --> 00:05:19,653 It just modifies whatever is existing in memory. 108 00:05:20,880 --> 00:05:24,960 Finally, there is another method called Extend. 109 00:05:24,960 --> 00:05:28,800 An Extend instead of an actual item or object 110 00:05:28,800 --> 00:05:31,320 takes what we call an iterable, 111 00:05:31,320 --> 00:05:33,480 which we're gonna get into later on 112 00:05:33,480 --> 00:05:36,360 but it's something that you can loop over, 113 00:05:36,360 --> 00:05:39,150 you can iterate over, which is a list. 114 00:05:39,150 --> 00:05:41,070 So we just give it another list, 115 00:05:41,070 --> 00:05:44,673 like let's say a hundred or 101. 116 00:05:46,230 --> 00:05:50,970 So if I run this, once again, it doesn't output a new list 117 00:05:50,970 --> 00:05:54,430 it just modifies the list in place and adds on 118 00:05:55,860 --> 00:05:58,260 or extends our list. 119 00:05:58,260 --> 00:06:01,203 And we can also just give it one item. 120 00:06:02,820 --> 00:06:07,053 All right, let's keep going with the removing methods. 121 00:06:08,160 --> 00:06:11,820 Now, with the removing, we once again 122 00:06:11,820 --> 00:06:14,880 have a few fun things that we can do. 123 00:06:14,880 --> 00:06:17,490 So let's continue with this basket 124 00:06:17,490 --> 00:06:19,050 that we've been using that now has 125 00:06:19,050 --> 00:06:21,690 a hundred included into it. 126 00:06:21,690 --> 00:06:24,540 And the way we can remove things is, 127 00:06:24,540 --> 00:06:25,980 well there's a few ways. 128 00:06:25,980 --> 00:06:30,213 First one is basket dot pop. 129 00:06:31,710 --> 00:06:33,183 And basket dot pop, 130 00:06:34,200 --> 00:06:36,030 If I go like this, 131 00:06:36,030 --> 00:06:39,150 and let's print this basket. 132 00:06:39,150 --> 00:06:42,406 I'm going to comment out the new list for now 133 00:06:42,406 --> 00:06:44,490 since we're not using it. 134 00:06:44,490 --> 00:06:47,670 And then we're going to say, basket dot pop, 135 00:06:47,670 --> 00:06:48,830 and then print... 136 00:06:50,820 --> 00:06:52,320 basket. 137 00:06:52,320 --> 00:06:53,320 Let's run this 138 00:06:54,510 --> 00:06:59,430 and pop pops off whatever is at the end of the list. 139 00:06:59,430 --> 00:07:00,900 In our case, at the end of the list, 140 00:07:00,900 --> 00:07:04,020 because we've extended the basket to a hundred, 141 00:07:04,020 --> 00:07:06,330 that a hundred gets removed. 142 00:07:06,330 --> 00:07:09,123 If I do basket dot pop again, 143 00:07:10,410 --> 00:07:13,147 whoop, not here, down here, 144 00:07:13,147 --> 00:07:18,147 and I run, you see that both 100 and 5 get removed. 145 00:07:19,290 --> 00:07:21,723 What if I do pop zero? 146 00:07:22,590 --> 00:07:23,913 If I click run here, 147 00:07:26,670 --> 00:07:28,930 it removes the item 148 00:07:29,850 --> 00:07:31,560 in the index. 149 00:07:31,560 --> 00:07:34,110 So here, pop zero is going to remove 150 00:07:34,110 --> 00:07:38,280 whatever's at index of zero, which is one. 151 00:07:38,280 --> 00:07:41,223 Now, there's also dot, remove. 152 00:07:42,330 --> 00:07:47,010 Remove, again, is, we give it a value 153 00:07:47,010 --> 00:07:48,450 that we wanna remove. 154 00:07:48,450 --> 00:07:52,140 So in our case, let's say we wanna remove number four. 155 00:07:52,140 --> 00:07:53,583 Well, if we run this, 156 00:07:55,710 --> 00:07:58,950 it's going to remove four for us. 157 00:07:58,950 --> 00:08:03,120 So remove, we give it the value that we wanna remove. 158 00:08:03,120 --> 00:08:07,053 With pop, we give it the index that we wanna remove. 159 00:08:07,890 --> 00:08:11,970 And just to see if this is working 160 00:08:11,970 --> 00:08:14,283 let's add a new list here, 161 00:08:15,330 --> 00:08:18,060 and we'll say new list 162 00:08:18,060 --> 00:08:19,773 equals basket dot remove. 163 00:08:20,730 --> 00:08:22,440 Add a new list here. 164 00:08:22,440 --> 00:08:24,063 See if that gets modified. 165 00:08:25,410 --> 00:08:27,240 Nope, it does not get modified. 166 00:08:27,240 --> 00:08:30,330 That means remove is working in place. 167 00:08:30,330 --> 00:08:34,980 It doesn't return a value, it just simply changes 168 00:08:34,980 --> 00:08:36,929 whatever list you give it. 169 00:08:36,929 --> 00:08:38,576 What if we do pop? 170 00:08:38,576 --> 00:08:40,950 If I run this, 171 00:08:40,950 --> 00:08:42,690 I get five. 172 00:08:42,690 --> 00:08:43,799 Hmm, 173 00:08:43,799 --> 00:08:45,960 why is that? 174 00:08:45,960 --> 00:08:48,750 And this is something that you just have to get used to. 175 00:08:48,750 --> 00:08:51,030 Different methods do different things. 176 00:08:51,030 --> 00:08:53,640 For example, pop, the way it works 177 00:08:53,640 --> 00:08:56,220 is that pop returns 178 00:08:56,220 --> 00:08:59,340 whatever you have just removed. 179 00:08:59,340 --> 00:09:01,080 In our case, when we did four 180 00:09:01,080 --> 00:09:05,310 that is index of 0, 1, 2, 3, 4. 181 00:09:05,310 --> 00:09:08,343 It returned the number five for me. 182 00:09:09,690 --> 00:09:12,120 Even though it removed it from the basket 183 00:09:12,120 --> 00:09:14,910 it still returned something. 184 00:09:14,910 --> 00:09:18,750 And the reason for others is that we got none. 185 00:09:18,750 --> 00:09:21,390 That is when a method doesn't return anything, 186 00:09:21,390 --> 00:09:24,453 it returns none, a topic we're gonna cover shortly. 187 00:09:25,710 --> 00:09:27,510 So you have to be careful 188 00:09:27,510 --> 00:09:30,210 and understand what each method returns. 189 00:09:30,210 --> 00:09:32,460 If I go over extend, 190 00:09:32,460 --> 00:09:35,010 I see here that this little arrow 191 00:09:35,010 --> 00:09:37,083 shows me that none is returned. 192 00:09:38,310 --> 00:09:42,120 That means it's not going to produce anything 193 00:09:42,120 --> 00:09:42,953 with this method. 194 00:09:42,953 --> 00:09:44,040 It's only going to change 195 00:09:44,040 --> 00:09:46,293 or extend a list that it was given. 196 00:09:47,580 --> 00:09:49,050 Don't worry if this is confusing. 197 00:09:49,050 --> 00:09:50,580 That's something that you're gonna get used to 198 00:09:50,580 --> 00:09:52,710 as we get more and more practice. 199 00:09:52,710 --> 00:09:56,100 Now, the last removing method I wanna show you 200 00:09:56,100 --> 00:09:57,750 is clear 201 00:09:57,750 --> 00:10:00,630 And clear, as you might guess. 202 00:10:00,630 --> 00:10:02,133 If I click run here, 203 00:10:03,450 --> 00:10:05,400 well, this is none 204 00:10:05,400 --> 00:10:09,843 but if I go to basket and I click run, 205 00:10:11,760 --> 00:10:13,890 the basket is empty. 206 00:10:13,890 --> 00:10:17,070 Clear removes whatever's in the list. 207 00:10:17,070 --> 00:10:21,093 Completely clears it just as a name suggests. 208 00:10:22,200 --> 00:10:24,930 All right, that was a lot, but there's still a few more. 209 00:10:24,930 --> 00:10:25,890 So let's take a break 210 00:10:25,890 --> 00:10:27,510 and then I'll see you in the next video. 211 00:10:27,510 --> 00:10:28,343 Bye-bye.