Table of Contents
Python list methods.
1. append( )
This method is used to append or say insert an element into the existing list. It takes only one value as an argument.
Code:
Output:
The new value is always inserted at the end of the list. Also, we can pass iterative objects into list or say list into list using this same method.
Code:
Output:
2. clear( )
This method is simply used to clear the list items and it returns an empty list. Also, this method takes no argument.
Code:
Output:
3. copy( )
It simply copy the whole list into a new variable. The copy method does not take any arguments.
Code:
Output:
4. count( )
Returns the number of times an element is present in the list. Also, it takes one parameter as argument and that is the value itself.
Code:
Output:
5. extend( )
This method ads and an iterable object (list, tuple, string) at the end of the current list. This method takes one parameter as an argument that is the iterable object.
Code:
Output:
Also, note that if we extend or give string value directly to the extend method then it will add 1 value of the string in each iteration and not the whole string all at once.
Code:
Output:
6. index( )
This method returns the index value of the first occurrence of any element. Also, this method takes one parameter as an argument and that is the element itself.
Code:
Output:
The reason we get output as “0“is because index method encounters “Apple” at 0th position and once it finds the desired input it terminates the execution irrespective of duplicate elements present in it or not.
Now if we print give “Lion” as input we will get different output
Code:
Output:
Simply put this method returns the index value of particular element and if that element is not present in that list it will return value error.
7. insert( )
This method ads an element at the desired position. It takes two value as argument, the first value is position and the second value is the input.
Code:
Output:
Note that car is inserted at the 2nd position in the list, “Apple” being at 0th position. Now we can also insert the iterable object.
Code:
Output:
See that l2 is inserted at the 1st position inside list “l1”.
8. pop( )
This method removes and delete element. Now the argument we passed here is optional. If we do not give any argument into pop method it will by default removes and delete the last element from the list.
pop without any argument
code:
Output:
Now we can give parameter as argument in the pop method but pop method only and only accepts the integer value as argument.
pop with argument
Code:
Output:
Now note that integer value is treated as the position of the element in the list.
9. remove( )
This method removes and delete the element by taking directly the element as value. Now if there are duplicate values in the list it will remove and delete the first occurrence of element.
Without any duplicate value
Code:
Output:
With duplicate value
Code:
Output:
Note that the first occurrence of the element is deleted and the rest is as it is.
10. reverse( )
This method simply reverses the element into the list, it does not take any value
As argument.
Code:
Output:
11. sort ( )
This method sorts the list in an ordered fashion also note that it can only sort Integer value not the string. By default, it sorts the list into ascending order to make it sort in descending we need to use reverse method.
Ascending order sort (by default)
Code:
Output:
Descending order sort
Code:
Output: