What Are List/Array Methods | Types Of Methods In Python List.

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:

append code

Output:

apend 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:

python list apend code

Output:

what is list in python


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:

python list code

Output:

python list clear code


3. copy( )

It simply copy the whole list into a new variable. The copy method does not take any arguments.

Code:

list copy

Output:

python list clear code


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:

count code list python

Output:

count 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:

extend code in python list

Output:

extend 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:

extend code

Output:

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:

index code

Output:

list 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:

index 2

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:

insert code

Output:

insert 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:

insert code 2

Output:

insert output 2

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:

pop code in python list

Output:

pop 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:

pop code 2

Output:

pop output in python

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:

remove code in python list

Output:

remove output in python

With duplicate value

Code:

With duplicate value remove

Output:

remove output with duplicate value

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:

reverse code

Output:

reverse 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:

python list sort code

Output:

sort output in python list

Descending order sort

Code:

sort code

Output:

sort output

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published.