Python List
We have already seen What List is? and how to write elements in List.
To follow that post visit Python Data Types. Where you will get all the different data types available into Python programming.
In this blog we will see different methods to work on list, this can be like appending a item to list, removing item from list, How to find length of a list and so on....
Syntax:
name_of_an_object . name_of_the_method
example:
x = [2,5,3,8,6,9]
x.sort ( )
Here you can see x is the object of type 'List' and sort is the method to perform on object.
Below given is the list of methods that can be executed on list type of object.
We will see each one with an example.
To follow that post visit Python Data Types. Where you will get all the different data types available into Python programming.
In this blog we will see different methods to work on list, this can be like appending a item to list, removing item from list, How to find length of a list and so on....
What is Method?
Method is basically a function which is written for specific object.Syntax:
name_of_an_object . name_of_the_method
example:
x = [2,5,3,8,6,9]
x.sort ( )
Here you can see x is the object of type 'List' and sort is the method to perform on object.
Below given is the list of methods that can be executed on list type of object.
We will see each one with an example.
list.append(x)
|
Add an item to the end of the list. Equivalent to a[len(a):] = [x].
|
list.extend(L)
|
Extend the list by appending all the items in the given list. Equivalent to a[len(a):] = L.
|
list.insert(i, x)
|
Insert an item at a given position. The first argument is the index of the element to insert.
|
list.remove(x)
|
Remove the first item from the list whose value is x. It is an error if there is no such item.
|
list.copy()
|
Return a shallow copy of the list. Equivalent to a[:].
|
list.pop([i])
|
Remove the item at the given position and return it. If no index is specified, removes the last item.
|
list.clear()
|
Remove all items from the list. Equivalent to del a[:].
|
list.index(x)
|
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
|
list.count(x)
|
Return the number of times x appears in the list.
|
list.reverse()
|
Reverse the elements of the list in place.
|
list.sort(key=None, reverse=False)
|
Sort the items of the list in place (the arguments can be used for sort customization).
|
List Methods With Examples
list.append(x)
This method adds an item to the end of the list.
It is equivalent to doing: a[len(a):] = [x]
The result would be:
list.extend(x)
This method extends the list by appending all the items in the given list.
It is equivalent to doing: a[len(a):] = L
The result would be:
list.insert(i, x)
This method inserts an item at a given position in the list. Where:
i : The index at which the item is to be inserted.
x : The item that is to be inserted.
The result would be:
list.remove(x)
This method removes the first item from the list whose value is x. It is an error if there is no such item.
The result would be:
list.pop([i])
This method removes the item at the given position in the list and returns it.
If no index is specified , list.pop() removes the last item in the list.
Note that the square brackets around the i in the method signature denotes that the parameter is optional, not that you should type square brackets at the position.
The result would be:
list.clear()
This method removes all the items from the list.
It is equivalent to doing:
del a[:]
The result would be:
list.index(x)
This method returns the index in the list of the first item whose value is x. It is an error if there is not such item.
The result would be:
list.count(x)
This method returns the number of times x appears in the list
The result would be:
list.sort(key=None, reverse=False)
This method sorts the items of the list in place. Note that this method does not return anything. (The arguments are optional and can be used for sort customization, see sorted() for their explanation.)
The result would be:
list.reverse()
This method reverses the items of the list in place. Note that this method does not return anything.
The result would be:
list.copy()
This method returns a shallow copy of the list.
It is equivalent to doing:
a[:]
The result would be:
I hope you may have understood list and methods that can perform of list.
Try above functions with other examples and try to use it in various examples.
Thank you.
Looking for the Genuine Experience Certificate with 100% Complete Verification So Contact Experienced Letter Solutions Consulting Firms Which Provide Experienced Letter with100 % Guaranteed. Contact Now- 9599119376. Or Visit Website- https://experincedletter.blogspot.com/
ReplyDelete