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