1 00:00:00,560 --> 00:00:02,400 - [Man] In this video we're going to take a look 2 00:00:02,400 --> 00:00:05,920 at a variety of other list methods that you have available 3 00:00:05,920 --> 00:00:08,500 to you and a couple of times now, I've shown you 4 00:00:08,500 --> 00:00:11,913 that when you type a list name and then a dot and a Tab, 5 00:00:11,913 --> 00:00:15,880 Ipython will show you the complete list of methods that 6 00:00:15,880 --> 00:00:19,180 you're allowed to call on that list object. 7 00:00:19,180 --> 00:00:22,270 So for this example or series of examples, rather, 8 00:00:22,270 --> 00:00:25,420 let's start with a list called color names 9 00:00:25,420 --> 00:00:29,230 and let's also begin by demonstrating various ways 10 00:00:29,230 --> 00:00:32,260 to add elements to a list. 11 00:00:32,260 --> 00:00:34,090 So to begin, 12 00:00:34,090 --> 00:00:35,460 we're going to use 13 00:00:35,460 --> 00:00:38,970 the insert method, which as its name implies, 14 00:00:38,970 --> 00:00:40,610 is going to insert an item 15 00:00:40,610 --> 00:00:43,370 into the list and the way it works is you specify 16 00:00:43,370 --> 00:00:46,470 the index number where you'd like to place a value 17 00:00:46,470 --> 00:00:49,660 and let's say we want to insert red in this list. 18 00:00:49,660 --> 00:00:54,070 The index number is the point at which the new value 19 00:00:54,070 --> 00:00:56,707 will be placed; whatever is already there will be, 20 00:00:56,707 --> 00:01:00,837 if you will, shifted to the right in the collection 21 00:01:00,837 --> 00:01:04,080 and will now have a index one higher than that. 22 00:01:04,080 --> 00:01:07,341 So at index zero, right now is orange, 23 00:01:07,341 --> 00:01:11,930 it's now going to be be index one in the updated list. 24 00:01:11,930 --> 00:01:14,827 So if I go ahead and display color names after 25 00:01:14,827 --> 00:01:18,350 that operation, we can see index zero is now red 26 00:01:18,350 --> 00:01:21,420 and index one is now orange. 27 00:01:21,420 --> 00:01:24,280 Now, another way to add... 28 00:01:24,280 --> 00:01:26,460 one item to the list is 29 00:01:26,460 --> 00:01:30,520 with the append method and as its name implies, 30 00:01:30,520 --> 00:01:34,060 it's going to add that to the end of the list. 31 00:01:34,060 --> 00:01:38,134 So if I go ahead and say I wanna add the object blue 32 00:01:38,134 --> 00:01:42,801 to the end of the list, I can then reevaluate color names 33 00:01:42,801 --> 00:01:45,910 and now we see that the append operation 34 00:01:45,910 --> 00:01:49,160 did indeed place an item at the end of the list. 35 00:01:49,160 --> 00:01:53,640 Now, both of these methods insert one object into the list. 36 00:01:53,640 --> 00:01:55,640 In the first case wherever you want. 37 00:01:55,640 --> 00:01:58,411 In the second case only at the end. 38 00:01:58,411 --> 00:02:02,530 Separately, you have the ability to add a whole bunch 39 00:02:02,530 --> 00:02:04,630 of items to the end of a list 40 00:02:04,630 --> 00:02:06,860 and there's a couple of ways to that. 41 00:02:06,860 --> 00:02:09,651 One of which, is the extend method. 42 00:02:09,651 --> 00:02:12,790 So actually for this one, let me copy and paste 43 00:02:12,790 --> 00:02:14,912 a snippet of code to execute. 44 00:02:14,912 --> 00:02:18,420 So here what we have is our list color names, 45 00:02:18,420 --> 00:02:21,650 the extend method that we'd like to call and the argument 46 00:02:21,650 --> 00:02:26,020 to this method is a sequence of objects that you w like 47 00:02:26,020 --> 00:02:30,030 to add to the end of the list and what would happen in this 48 00:02:30,030 --> 00:02:33,830 case, is each individual item, within that sequence, 49 00:02:33,830 --> 00:02:37,160 will be placed at the end of the list color names. 50 00:02:37,160 --> 00:02:40,600 So if I go ahead and execute that and then, once again, 51 00:02:40,600 --> 00:02:44,470 reevaluate color names, you can see that both indigo 52 00:02:44,470 --> 00:02:47,054 and violet were added to the end of list. 53 00:02:47,054 --> 00:02:51,120 I did not take the entire list object 54 00:02:51,120 --> 00:02:54,940 and add that as one item into the list. 55 00:02:54,940 --> 00:02:57,710 It actually iterates through the elements, 56 00:02:57,710 --> 00:03:01,150 taking one item at a time from the sequence argument 57 00:03:01,150 --> 00:03:03,102 and adding them to the end. 58 00:03:03,102 --> 00:03:06,373 Now this is actually the equivalent of using 59 00:03:06,373 --> 00:03:09,343 the plus equals operator. 60 00:03:10,230 --> 00:03:11,710 Let's go ahead and look at this 61 00:03:11,710 --> 00:03:15,450 extend method in a little bit more detail. 62 00:03:15,450 --> 00:03:19,700 Let's start with a list that's empty for example 63 00:03:19,700 --> 00:03:21,680 and let's demonstrate what happens 64 00:03:21,680 --> 00:03:24,344 when you extend a list with a string. 65 00:03:24,344 --> 00:03:27,787 So let' say we have a string that's just ABC, 66 00:03:27,787 --> 00:03:30,200 for demonstration purposes here, 67 00:03:30,200 --> 00:03:33,710 and we want to add 'em to the end of the sample list. 68 00:03:33,710 --> 00:03:36,660 So we'll use the extend method once again 69 00:03:36,660 --> 00:03:39,350 and we'll give it the sequence S as an argument. 70 00:03:39,350 --> 00:03:43,420 So we can see what the sample list looks like after 71 00:03:43,420 --> 00:03:45,840 that operation and you see that it picks off 72 00:03:45,840 --> 00:03:49,027 each individual entity within the sequence, 73 00:03:49,027 --> 00:03:52,600 that you pass as an argument, and the same thing 74 00:03:52,600 --> 00:03:55,460 would be true by the way with a tuple. 75 00:03:55,460 --> 00:03:59,360 So if I have a tuple called T and I give it a few values 76 00:03:59,360 --> 00:04:02,660 and then I say to sample list 77 00:04:02,660 --> 00:04:05,650 to extend with T as an argument, 78 00:04:05,650 --> 00:04:07,470 it's going to pick off the one, the two 79 00:04:07,470 --> 00:04:11,860 and the three and add them to the end of the sample list. 80 00:04:11,860 --> 00:04:14,950 So you have one list with three elements that came from 81 00:04:14,950 --> 00:04:19,050 the string and three elements that came from that tuple. 82 00:04:19,050 --> 00:04:23,200 Now, in each of these two cases we took a variable 83 00:04:23,200 --> 00:04:25,080 that represented a sequence 84 00:04:25,080 --> 00:04:28,680 and passed it into the extend method. 85 00:04:28,680 --> 00:04:30,300 What if you just wanted to pass 86 00:04:30,300 --> 00:04:33,430 a tuple directly to the extend method? 87 00:04:33,430 --> 00:04:37,680 So, let's say we want to call sample list 88 00:04:37,680 --> 00:04:40,150 and I'm not going to execute this yet 89 00:04:40,150 --> 00:04:41,770 but let's just type it first. 90 00:04:41,770 --> 00:04:44,050 So, let's say I wanna add four, five and six. 91 00:04:44,050 --> 00:04:45,786 Whoops, I forgot the extend call. 92 00:04:45,786 --> 00:04:47,350 (keyboard clicking) 93 00:04:47,350 --> 00:04:49,720 So, let's say we wanna add four, five and six 94 00:04:49,720 --> 00:04:53,100 to the end of sample list, you might be tempted to write 95 00:04:53,100 --> 00:04:57,510 the expression this way but the extend method requires one 96 00:04:57,510 --> 00:05:02,510 argument only and that argument must be sequence of values. 97 00:05:02,550 --> 00:05:06,910 So if you accidentally pass in more than one element, 98 00:05:06,910 --> 00:05:09,010 which is what I'm doing here, you would actually 99 00:05:09,010 --> 00:05:13,040 get a type error, if you were to execute this code. 100 00:05:13,040 --> 00:05:17,870 So again, it expects one object to be passed as an argument. 101 00:05:17,870 --> 00:05:22,280 So if you want to pass a tuple of values into the extend 102 00:05:22,280 --> 00:05:24,480 method, you would have to actually wrap them 103 00:05:24,480 --> 00:05:27,100 in an additional set of parentheses. 104 00:05:27,100 --> 00:05:30,440 So now what we have here, inside the parentheses, 105 00:05:30,440 --> 00:05:34,320 is actually a tuple containing four, five and six. 106 00:05:34,320 --> 00:05:38,540 We're passing one object to the extend method, a tuple, 107 00:05:38,540 --> 00:05:42,050 it's then iterating through the tuple to evaluate 108 00:05:42,050 --> 00:05:45,015 the elements and we can see that we're able to add 109 00:05:45,015 --> 00:05:48,334 four, five and six to the end of the list. 110 00:05:48,334 --> 00:05:51,750 So, at this point we've shown you that you can 111 00:05:51,750 --> 00:05:55,860 insert and append items to a list. 112 00:05:55,860 --> 00:05:59,480 Now let's talk about removing items from a list. 113 00:05:59,480 --> 00:06:03,000 So for example, if we go ahead and, whoops, 114 00:06:03,000 --> 00:06:05,618 if we say color_names.remove 115 00:06:05,618 --> 00:06:07,480 (keyboard clicking) and let's say we 116 00:06:07,480 --> 00:06:10,050 want to remove the string green, 117 00:06:10,050 --> 00:06:12,610 so we're talking about the color names list up above here 118 00:06:12,610 --> 00:06:16,160 once again, and you can see the string green is in there. 119 00:06:16,160 --> 00:06:19,240 So we have this remove method for that purpose 120 00:06:19,240 --> 00:06:23,063 and it's going to remove the very first item it encounters 121 00:06:23,063 --> 00:06:27,830 that has that value and of course we only have one such item 122 00:06:27,830 --> 00:06:32,140 in this particular case and now if we evaluate color names, 123 00:06:32,140 --> 00:06:36,470 after that remove operation, indeed, green is gone. 124 00:06:36,470 --> 00:06:41,470 Now this operation also will result in a value error, 125 00:06:41,510 --> 00:06:46,500 if the specified argument is not found within the list. 126 00:06:46,500 --> 00:06:49,490 So you want to be aware of that issue. 127 00:06:49,490 --> 00:06:52,280 Now, you can also completely empty out a list. 128 00:06:52,280 --> 00:06:55,600 We demonstrated a way to do that using slicing 129 00:06:55,600 --> 00:06:59,810 in a proceeding video but there's also the clear method. 130 00:06:59,810 --> 00:07:04,090 So I can say color_names.clear and if I then evaluate 131 00:07:04,090 --> 00:07:06,530 color names after that operation, you can see 132 00:07:06,530 --> 00:07:09,500 there's no elements left in the list. 133 00:07:09,500 --> 00:07:11,500 And that would have been the equivalent 134 00:07:11,500 --> 00:07:13,513 of saying color_names[:]. 135 00:07:15,790 --> 00:07:18,210 So, just an empty set of square brackets 136 00:07:18,210 --> 00:07:21,680 with a colon in the middle and we could've assigned 137 00:07:21,680 --> 00:07:24,846 to that the empty list and that's the equivalent 138 00:07:24,846 --> 00:07:27,990 of calling clear and this case they happen 139 00:07:27,990 --> 00:07:30,800 to take up the same number of characters, 140 00:07:30,800 --> 00:07:33,550 so there you can really choose either way 141 00:07:33,550 --> 00:07:37,040 and use the one that's your preference. 142 00:07:37,040 --> 00:07:39,210 Okay, so we've looked at a number 143 00:07:39,210 --> 00:07:40,680 of different methods so far. 144 00:07:40,680 --> 00:07:44,660 Next, what we'd like to do is talk about counting up 145 00:07:44,660 --> 00:07:49,330 the number of occurrences of a given item within a list. 146 00:07:49,330 --> 00:07:54,080 So, for this purpose I'm going to paste in a responses 147 00:07:54,080 --> 00:07:57,550 list that has a whole bunch of numeric values 148 00:07:57,550 --> 00:08:01,960 from one through five in it and these might be responses 149 00:08:01,960 --> 00:08:05,710 to a survey, for example, and then what we want 150 00:08:05,710 --> 00:08:08,330 to do is just total up those responses. 151 00:08:08,330 --> 00:08:11,570 So, I'm going to use a little for loop for that purpose 152 00:08:11,570 --> 00:08:14,670 and I'll paste it in and then explain it to you. 153 00:08:14,670 --> 00:08:18,290 So here we have a for loop where the target variable I 154 00:08:18,290 --> 00:08:21,000 is going to be a value from one through five. 155 00:08:21,000 --> 00:08:24,674 Those happen to be the values in my responses list 156 00:08:24,674 --> 00:08:27,180 and for every one of those items we're going 157 00:08:27,180 --> 00:08:31,719 to say the item, whatever I is at this point appears 158 00:08:31,719 --> 00:08:33,840 and then we're going to figure out 159 00:08:33,840 --> 00:08:37,880 how many times it appears in the responses list. 160 00:08:37,880 --> 00:08:42,342 So response.count is the key part of this print statement. 161 00:08:42,342 --> 00:08:46,670 Responses.count with the argument I says count up how many 162 00:08:46,670 --> 00:08:49,582 times the value I appears in that list. 163 00:08:49,582 --> 00:08:53,550 And for each of those items we can see how many times 164 00:08:53,550 --> 00:08:56,320 each of those occur and of course you can go back 165 00:08:56,320 --> 00:08:58,630 and count them up yourself to confirm 166 00:08:58,630 --> 00:09:01,890 that these count values are, in fact, correct. 167 00:09:01,890 --> 00:09:04,810 Now count, by the way, was one of those descriptive 168 00:09:04,810 --> 00:09:08,960 statistics that we introduced in an earlier lesson, as well. 169 00:09:08,960 --> 00:09:12,990 So if you a list full of a large number items 170 00:09:12,990 --> 00:09:15,910 and you were trying to do a little bit of descriptive 171 00:09:15,910 --> 00:09:18,200 statistics on the list, one of the things 172 00:09:18,200 --> 00:09:21,123 you might do is something like this for loop here. 173 00:09:22,330 --> 00:09:24,990 Now a couple more items that we have for lists, 174 00:09:24,990 --> 00:09:28,800 we also have the ability to reverse a lists elements. 175 00:09:28,800 --> 00:09:32,890 So let's start by redefining color names list here 176 00:09:32,890 --> 00:09:36,100 and I'm mainly doing this so that we can see it close 177 00:09:36,100 --> 00:09:38,880 to the snippet of code that I'm about to execute. 178 00:09:38,880 --> 00:09:42,210 So we can see the order of the strings at the moment. 179 00:09:42,210 --> 00:09:47,190 If I go and say color_name.reverse ... 180 00:09:47,190 --> 00:09:50,690 that's going to reserve the items in the list in place. 181 00:09:50,690 --> 00:09:55,180 It's modifying the list, so if I do that and then evaluate 182 00:09:55,180 --> 00:09:57,550 color names you can see, indeed, 183 00:09:57,550 --> 00:10:00,350 they have been reversed inside that list. 184 00:10:00,350 --> 00:10:03,110 So that's a convenient way to quickly reverse 185 00:10:03,110 --> 00:10:07,230 the items in a collection, in a list, specifically. 186 00:10:07,230 --> 00:10:09,750 And then finally there's one other method 187 00:10:09,750 --> 00:10:12,760 that we want to point out in this video. 188 00:10:12,760 --> 00:10:15,460 The method copy, so let's create 189 00:10:15,460 --> 00:10:18,290 a copied list variable for a moment. 190 00:10:18,290 --> 00:10:22,160 If I say color_names.copy, this is going 191 00:10:22,160 --> 00:10:26,979 to perform a shallow copy of the color names list. 192 00:10:26,979 --> 00:10:29,870 What that means is that I will get another list 193 00:10:29,870 --> 00:10:32,900 with the same number of elements as color names 194 00:10:32,900 --> 00:10:36,440 and the elements will refer to the same objects 195 00:10:36,440 --> 00:10:38,525 that color names refers to. 196 00:10:38,525 --> 00:10:42,450 So, blue, green, yellow, orange and red as strings 197 00:10:42,450 --> 00:10:45,620 will exist in memory and both the color names list 198 00:10:45,620 --> 00:10:50,620 and the copied list are going to refer to the same elements. 199 00:10:50,800 --> 00:10:55,620 So if now go and say copied_list, you can see we have 200 00:10:55,620 --> 00:10:58,340 what appears to be a copy of the original list 201 00:10:58,340 --> 00:11:01,980 and remember you can always go and use that ID function 202 00:11:01,980 --> 00:11:05,508 to confirm that we really do have the same object. 203 00:11:05,508 --> 00:11:09,713 So, for example, if go and say ID of color_names[0], 204 00:11:11,335 --> 00:11:14,330 we can see what the ID number of color names is for that 205 00:11:14,330 --> 00:11:18,430 and If I repeat that for the copied list... 206 00:11:19,510 --> 00:11:23,180 I add element zero, we can see we get the exact same 207 00:11:23,180 --> 00:11:27,070 ID value, which is further confirmation that both of these 208 00:11:27,070 --> 00:11:31,893 lists have elements referring to the same objects in memory.