1 00:00:00,150 --> 00:00:09,930 In this tutorial we will talk about the different list method Python has several built in methods that 2 00:00:09,930 --> 00:00:12,400 can be used to modify lists. 3 00:00:13,740 --> 00:00:18,590 We will discuss some of these methods using some examples. 4 00:00:20,020 --> 00:00:27,500 This is the syntax for calling a method on an object Object dart method. 5 00:00:28,670 --> 00:00:30,730 Open Close parentheses. 6 00:00:30,770 --> 00:00:33,980 The object here would be the list 7 00:00:36,650 --> 00:00:42,400 and the list and the method is separated by the dot operator. 8 00:00:43,600 --> 00:00:54,640 And then the method say for example I want to call the append method the append method is used to add 9 00:00:54,940 --> 00:00:58,850 a single item to the end of the list. 10 00:00:58,960 --> 00:01:04,720 Say for example I want to add a number to the list. 11 00:01:04,870 --> 00:01:05,770 A list you 1 12 00:01:08,760 --> 00:01:11,160 so to the append method. 13 00:01:11,310 --> 00:01:18,360 I have passed the argument for the value forward and this value has to be added to the end of the list 14 00:01:19,160 --> 00:01:20,370 a list D1. 15 00:01:20,380 --> 00:01:26,960 So after this line of code is executed in your Jupiter book. 16 00:01:27,120 --> 00:01:30,950 This element is added to the end of the list so we will check this. 17 00:01:30,960 --> 00:01:43,300 Indeed you put book so here I have defined the list list 1 and now I'm going to apply the append method 18 00:01:43,420 --> 00:01:54,750 on this list and I'm going to parse the argument for now the append method modifies the list in place 19 00:01:55,320 --> 00:02:03,440 so it does not create a new list but it modifies the existing list that does list 1. 20 00:02:03,450 --> 00:02:08,800 So now let's display the list 1. 21 00:02:08,970 --> 00:02:17,490 So as you can see the append function has added a single item to the list. 22 00:02:19,070 --> 00:02:24,650 If we tried to add another list to an existing list. 23 00:02:24,650 --> 00:02:27,940 So what does the append method do. 24 00:02:28,130 --> 00:02:32,570 So for example this is my list a list to one. 25 00:02:32,630 --> 00:02:34,730 Let us call the append Matar. 26 00:02:35,660 --> 00:02:40,760 So I'm going to append a list to the existing list a list. 27 00:02:40,770 --> 00:02:41,470 1. 28 00:02:41,540 --> 00:02:49,490 So my new list has the element 8 some twenty three forty five. 29 00:02:49,820 --> 00:02:58,560 Now after you execute this line of code and then display the list a list is 1 as you can see here. 30 00:02:58,700 --> 00:03:11,000 D append matter come to does the argument fast to the append method as one single item and this item 31 00:03:11,180 --> 00:03:18,020 has been added to the end of the list as one single item. 32 00:03:18,020 --> 00:03:31,070 Next we will talk about the extend matter the extend method pigs and I did a bit and and each element 33 00:03:31,220 --> 00:03:39,740 of the item a bill to a list one at a time so let us understand what an ideal is. 34 00:03:40,910 --> 00:03:51,710 And I did it Bill is an object which we can either it or would do either it or word or sequence means 35 00:03:51,710 --> 00:04:03,090 to go to each of the elements of the sequence and perform some kind of operation strings tuples set 36 00:04:03,210 --> 00:04:16,890 lists are an examples of I dribbled the extend metaphor add element to a list by adding on all the elements 37 00:04:17,010 --> 00:04:20,680 of the item a bill that we passed to it. 38 00:04:20,760 --> 00:04:32,380 So once you execute the scored each of the elements of list three minus two zero and four are added 39 00:04:32,380 --> 00:04:39,180 to list to select execute this in the Jupiter notebook. 40 00:04:39,570 --> 00:04:51,790 So here I have to list this too and list three I'm going to apply the extend method on list to the extend 41 00:04:51,800 --> 00:05:02,820 metaphor takes the argument list three goes to each of the elements it lists three and add them to the 42 00:05:02,820 --> 00:05:04,210 list too. 43 00:05:04,380 --> 00:05:15,750 So let's execute this in and then display the list to as you can see here that all of these items have 44 00:05:15,750 --> 00:05:25,800 been added individually into list to to whereas consider the append method if you pass a list to the 45 00:05:25,830 --> 00:05:36,420 append method it append the entire list to the end of the existing list as a single item coming to the 46 00:05:36,480 --> 00:05:37,890 extend method. 47 00:05:38,040 --> 00:05:45,440 The extend method add each of the items individually to the list. 48 00:05:45,600 --> 00:05:54,360 Next we have just discussed about the extend method we have talked that the extend method takes and 49 00:05:54,480 --> 00:06:03,640 I editable and and each element of the item a bill to the list one at a time and thus either a bill 50 00:06:03,640 --> 00:06:07,840 can be a list set tuple or a string. 51 00:06:07,840 --> 00:06:13,600 So we have already tried to add a list to another list using the extend method. 52 00:06:13,600 --> 00:06:19,660 Now let's add a string to a list using the extend method. 53 00:06:19,960 --> 00:06:32,380 So here I have my list 3 and then I'm going to apply the extend method on the list 3 and the argument 54 00:06:32,440 --> 00:06:36,200 I have passed is a string which is an item. 55 00:06:36,910 --> 00:06:44,160 So let execute this cell and then verify list three. 56 00:06:44,240 --> 00:06:56,690 Okay so as we have discussed before the extend method goes to each of the characters in the string and 57 00:06:57,350 --> 00:06:59,080 added to the list. 58 00:07:00,600 --> 00:07:07,200 Similar to what has happened when we added to list using the extend method. 59 00:07:08,230 --> 00:07:13,660 So it retrieves each of the characters in the string. 60 00:07:13,850 --> 00:07:18,530 The first one is the character D and then or. 61 00:07:18,710 --> 00:07:24,040 And then G using the append and the extend method. 62 00:07:24,050 --> 00:07:28,730 We have seen that the lists have been modified in place. 63 00:07:28,730 --> 00:07:34,700 We can also concatenate two lists using the plus operator. 64 00:07:35,790 --> 00:07:44,100 So that the original lists are not modified the lists are concatenated and a new list is created. 65 00:07:44,130 --> 00:07:46,390 So let's see how that works. 66 00:07:46,410 --> 00:07:53,330 Let us concatenate the list list 2 and list three using the plus operator. 67 00:07:53,400 --> 00:07:56,230 So let's execute this line of code. 68 00:07:56,310 --> 00:08:05,350 So as you can see the list I listed 2 has been concatenated with list 3 and this is the result. 69 00:08:05,640 --> 00:08:10,160 Now let's display the list 2 and list 3. 70 00:08:10,260 --> 00:08:12,300 So this has not been modified. 71 00:08:12,360 --> 00:08:15,420 List 3 has not been modified as well. 72 00:08:15,510 --> 00:08:23,560 So when we can coordinate to list using the plus operator a new list is created. 73 00:08:23,580 --> 00:08:34,770 Next is the insert matter the Insert method inserts an item at a specified position within the given 74 00:08:34,770 --> 00:08:40,140 list and this is the syntax for the Insert method. 75 00:08:40,140 --> 00:08:49,770 The argument I hear is the index of the element before which to insert the Element X. 76 00:08:49,860 --> 00:08:58,550 I represent the index and X is the element which needs to be inserted into the list. 77 00:08:58,590 --> 00:08:59,070 Okay. 78 00:08:59,250 --> 00:09:13,520 This is list 3 and I'm going to insert the element mindless 7 at index 3 so let's execute this. 79 00:09:13,550 --> 00:09:25,770 Say and then display list three as you can see here the element minus seven has been inserted at index 80 00:09:26,180 --> 00:09:26,900 three. 81 00:09:26,970 --> 00:09:37,350 Next is the removed method the remove method searches for an item that is passed forward as an argument 82 00:09:37,440 --> 00:09:45,350 and removes the first matching item from the list so let's testis in the Jupiter notebook. 83 00:09:45,460 --> 00:09:52,690 So I want to remove the element forward from list three past the value forward as an argument to the 84 00:09:52,720 --> 00:09:53,470 removed method. 85 00:09:54,130 --> 00:09:59,030 Let's execute this line of code and then display list 3. 86 00:09:59,260 --> 00:10:04,700 The removed method removes the first matching item from the list. 87 00:10:04,900 --> 00:10:16,240 Next is index the index method returns the smallest list index of an element in the list and list three. 88 00:10:16,240 --> 00:10:26,440 Say for example I want to know the index of the character or the index method returns the index of that 89 00:10:26,440 --> 00:10:28,210 particular string. 90 00:10:28,300 --> 00:10:30,790 Next is the count method. 91 00:10:30,790 --> 00:10:35,310 We have seen the similar method for strings as well. 92 00:10:35,500 --> 00:10:42,110 And the account method returns the number of occurrences of an element in a list. 93 00:10:42,280 --> 00:10:54,130 Next as the pop method the pop method removes an individual item and returns to the pop method takes 94 00:10:54,130 --> 00:10:57,130 the index number as an argument. 95 00:10:57,130 --> 00:11:05,860 Say for example we want to remove the string D from the list 3 and it is it index number 3. 96 00:11:05,890 --> 00:11:09,540 So it does remove these string D from the list. 97 00:11:09,570 --> 00:11:12,940 3 that now display lists 3. 98 00:11:13,210 --> 00:11:22,290 So the pop method has removed the string D from the list 3 and here's the list 3. 99 00:11:22,300 --> 00:11:31,360 Next is the reverse method the reverse method helps to reverse the order of element in a list. 100 00:11:31,360 --> 00:11:34,280 Next is the sort method. 101 00:11:34,450 --> 00:11:39,910 The sort method can be used to start a list in the ascending order. 102 00:11:39,910 --> 00:11:47,150 Next is the copy method the copy method returns a shallow copy of a list. 103 00:11:47,320 --> 00:11:50,800 So let's understand this with an example. 104 00:11:50,800 --> 00:11:57,830 Consider this list a list of 1 and in the memory list. 105 00:11:57,850 --> 00:12:00,870 One is pointing to the elements here. 106 00:12:01,940 --> 00:12:11,240 Now if I want to create another list with the same elements I can use the assignment operator I create 107 00:12:11,330 --> 00:12:24,420 a new list a list to assign it to a list do 1 so in this case a list to do and a list do you 1. 108 00:12:24,510 --> 00:12:29,950 Pointing to the same memory location. 109 00:12:29,960 --> 00:12:44,360 So when I try to modify list 1 say using the append method I add a new element to the list C I mean 110 00:12:44,370 --> 00:12:46,600 to parse number 7. 111 00:12:46,850 --> 00:12:58,870 Now since both a listing 1 and a listing 2 are pointing to the same memory location the memory location 112 00:12:58,870 --> 00:13:13,070 now would contain all of the elements 1 2 3 4 6 and then after the append function numbered 7 has been 113 00:13:13,130 --> 00:13:28,200 added to the end of the list now both list 1 and list 2 and pointing to the updated list or the modified 114 00:13:28,200 --> 00:13:31,410 list so let's check this in the Jupiter notebook. 115 00:13:32,040 --> 00:13:41,640 So here I have the list one defined and then using the assignment operator I've copied the entire list 116 00:13:41,730 --> 00:13:50,940 one 2 list to now to verify if list to 1 and 2 are pointing to the same memory location we can use the 117 00:13:51,360 --> 00:14:02,320 identity operator is list 2 and if this returns are true that means that pointing to the same memory 118 00:14:02,320 --> 00:14:05,190 location. 119 00:14:05,400 --> 00:14:07,640 So this is true. 120 00:14:07,830 --> 00:14:17,000 Now I am going to append a new element to the list to 1 and will look past value 7 as an argument. 121 00:14:17,010 --> 00:14:26,210 So now let's display list one so the element has been added to the end of the list. 122 00:14:26,210 --> 00:14:29,330 Now let's see if you bought the list. 123 00:14:29,330 --> 00:14:33,870 That is list 1 and list two are binding to this same memory location. 124 00:14:33,920 --> 00:14:43,130 After applying the append method so you can see both the lists have been modified after applying the 125 00:14:43,190 --> 00:14:45,860 append method on one of the lists. 126 00:14:46,310 --> 00:14:55,640 If your requirement is to change or modify only one list without affecting the other list you can use 127 00:14:55,640 --> 00:14:57,500 the copy method. 128 00:14:57,560 --> 00:15:05,170 This is called the shallow copy and this is the syntax for the copy method. 129 00:15:05,180 --> 00:15:15,610 This is the original list dot com copy open and close parentheses so here I have the list. 130 00:15:15,640 --> 00:15:30,670 1 I now create another list list 3 which is a copy or list one so list one dot copy and then execute 131 00:15:30,670 --> 00:15:31,450 the cell. 132 00:15:31,810 --> 00:15:35,730 Now let us display list 3. 133 00:15:37,070 --> 00:15:37,510 OK. 134 00:15:37,520 --> 00:15:41,250 They have the same set of elements. 135 00:15:41,280 --> 00:15:43,840 List 1 and list 3. 136 00:15:44,000 --> 00:15:54,670 Now let us verify if they are pointing to the same memory location list 3 is list 1 so at the turns 137 00:15:54,760 --> 00:16:02,330 of files in this case saying they're not pointing to the same memory location. 138 00:16:02,370 --> 00:16:17,700 Now let us modify list 1 by adding a new element to list 1 so let's display list 1. 139 00:16:17,860 --> 00:16:22,580 So a new element has been added to the end of the list. 140 00:16:22,630 --> 00:16:24,700 List 1 let us display list 3. 141 00:16:24,760 --> 00:16:35,500 If it has been modified by this method no list 3 has not been modified by the append method because 142 00:16:35,550 --> 00:16:39,840 they are pointing to 2 different memory addresses. 143 00:16:40,060 --> 00:16:44,310 And this brings us to the end of this tutorial list. 144 00:16:44,320 --> 00:16:45,010 Methods.