In this tutorial we will talk about the different list method Python has several built in methods that
can be used to modify lists.
We will discuss some of these methods using some examples.
This is the syntax for calling a method on an object Object dart method.
Open Close parentheses.
The object here would be the list
and the list and the method is separated by the dot operator.
And then the method say for example I want to call the append method the append method is used to add
a single item to the end of the list.
Say for example I want to add a number to the list.
A list you 1
so to the append method.
I have passed the argument for the value forward and this value has to be added to the end of the list
a list D1.
So after this line of code is executed in your Jupiter book.
This element is added to the end of the list so we will check this.
Indeed you put book so here I have defined the list list 1 and now I'm going to apply the append method
on this list and I'm going to parse the argument for now the append method modifies the list in place
so it does not create a new list but it modifies the existing list that does list 1.
So now let's display the list 1.
So as you can see the append function has added a single item to the list.
If we tried to add another list to an existing list.
So what does the append method do.
So for example this is my list a list to one.
Let us call the append Matar.
So I'm going to append a list to the existing list a list.
1.
So my new list has the element 8 some twenty three forty five.
Now after you execute this line of code and then display the list a list is 1 as you can see here.
D append matter come to does the argument fast to the append method as one single item and this item
has been added to the end of the list as one single item.
Next we will talk about the extend matter the extend method pigs and I did a bit and and each element
of the item a bill to a list one at a time so let us understand what an ideal is.
And I did it Bill is an object which we can either it or would do either it or word or sequence means
to go to each of the elements of the sequence and perform some kind of operation strings tuples set
lists are an examples of I dribbled the extend metaphor add element to a list by adding on all the elements
of the item a bill that we passed to it.
So once you execute the scored each of the elements of list three minus two zero and four are added
to list to select execute this in the Jupiter notebook.
So here I have to list this too and list three I'm going to apply the extend method on list to the extend
metaphor takes the argument list three goes to each of the elements it lists three and add them to the
list too.
So let's execute this in and then display the list to as you can see here that all of these items have
been added individually into list to to whereas consider the append method if you pass a list to the
append method it append the entire list to the end of the existing list as a single item coming to the
extend method.
The extend method add each of the items individually to the list.
Next we have just discussed about the extend method we have talked that the extend method takes and
I editable and and each element of the item a bill to the list one at a time and thus either a bill
can be a list set tuple or a string.
So we have already tried to add a list to another list using the extend method.
Now let's add a string to a list using the extend method.
So here I have my list 3 and then I'm going to apply the extend method on the list 3 and the argument
I have passed is a string which is an item.
So let execute this cell and then verify list three.
Okay so as we have discussed before the extend method goes to each of the characters in the string and
added to the list.
Similar to what has happened when we added to list using the extend method.
So it retrieves each of the characters in the string.
The first one is the character D and then or.
And then G using the append and the extend method.
We have seen that the lists have been modified in place.
We can also concatenate two lists using the plus operator.
So that the original lists are not modified the lists are concatenated and a new list is created.
So let's see how that works.
Let us concatenate the list list 2 and list three using the plus operator.
So let's execute this line of code.
So as you can see the list I listed 2 has been concatenated with list 3 and this is the result.
Now let's display the list 2 and list 3.
So this has not been modified.
List 3 has not been modified as well.
So when we can coordinate to list using the plus operator a new list is created.
Next is the insert matter the Insert method inserts an item at a specified position within the given
list and this is the syntax for the Insert method.
The argument I hear is the index of the element before which to insert the Element X.
I represent the index and X is the element which needs to be inserted into the list.
Okay.
This is list 3 and I'm going to insert the element mindless 7 at index 3 so let's execute this.
Say and then display list three as you can see here the element minus seven has been inserted at index
three.
Next is the removed method the remove method searches for an item that is passed forward as an argument
and removes the first matching item from the list so let's testis in the Jupiter notebook.
So I want to remove the element forward from list three past the value forward as an argument to the
removed method.
Let's execute this line of code and then display list 3.
The removed method removes the first matching item from the list.
Next is index the index method returns the smallest list index of an element in the list and list three.
Say for example I want to know the index of the character or the index method returns the index of that
particular string.
Next is the count method.
We have seen the similar method for strings as well.
And the account method returns the number of occurrences of an element in a list.
Next as the pop method the pop method removes an individual item and returns to the pop method takes
the index number as an argument.
Say for example we want to remove the string D from the list 3 and it is it index number 3.
So it does remove these string D from the list.
3 that now display lists 3.
So the pop method has removed the string D from the list 3 and here's the list 3.
Next is the reverse method the reverse method helps to reverse the order of element in a list.
Next is the sort method.
The sort method can be used to start a list in the ascending order.
Next is the copy method the copy method returns a shallow copy of a list.
So let's understand this with an example.
Consider this list a list of 1 and in the memory list.
One is pointing to the elements here.
Now if I want to create another list with the same elements I can use the assignment operator I create
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.
Pointing to the same memory location.
So when I try to modify list 1 say using the append method I add a new element to the list C I mean
to parse number 7.
Now since both a listing 1 and a listing 2 are pointing to the same memory location the memory location
now would contain all of the elements 1 2 3 4 6 and then after the append function numbered 7 has been
added to the end of the list now both list 1 and list 2 and pointing to the updated list or the modified
list so let's check this in the Jupiter notebook.
So here I have the list one defined and then using the assignment operator I've copied the entire list
one 2 list to now to verify if list to 1 and 2 are pointing to the same memory location we can use the
identity operator is list 2 and if this returns are true that means that pointing to the same memory
location.
So this is true.
Now I am going to append a new element to the list to 1 and will look past value 7 as an argument.
So now let's display list one so the element has been added to the end of the list.
Now let's see if you bought the list.
That is list 1 and list two are binding to this same memory location.
After applying the append method so you can see both the lists have been modified after applying the
append method on one of the lists.
If your requirement is to change or modify only one list without affecting the other list you can use
the copy method.
This is called the shallow copy and this is the syntax for the copy method.
This is the original list dot com copy open and close parentheses so here I have the list.
1 I now create another list list 3 which is a copy or list one so list one dot copy and then execute
the cell.
Now let us display list 3.
OK.
They have the same set of elements.
List 1 and list 3.
Now let us verify if they are pointing to the same memory location list 3 is list 1 so at the turns
of files in this case saying they're not pointing to the same memory location.
Now let us modify list 1 by adding a new element to list 1 so let's display list 1.
So a new element has been added to the end of the list.
List 1 let us display list 3.
If it has been modified by this method no list 3 has not been modified by the append method because
they are pointing to 2 different memory addresses.
And this brings us to the end of this tutorial list.
Methods.