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