Python list methods are a set of built-in methods that can be implemented on the lists. The Python list methods can be used according to the convenience of the user.
What are the list methods in Python?
There are various built-in Python list methods used for different purposes. These are mentioned below:
- append()-the append() is used to add an element to the end of the list.
- clear()- this function removes all the elements present in the list
- copy()- this function returns a copy of the list as requested by the user
- count()- this function counts and then returns the number of elements present in the list
- extend()- this function adds the elements present in a list or any iterable, to the end of the current list
- index()- this function returns the value of the index of the first element
- insert()- this function adds an element at the specified position mentioned by the user
- pop()- this function removes the element specified by the user at a given index.
- remove()- this function removes the value of a specified item mentioned by the user
- reverse()- this function reverses the order of the list given by the user.
- sort()- this function sorts the list given by the user
How do I see methods in Python?
The help command function in Python allows users to see a list of all the methods. This function allows the user to see all of the key methods connected with a particular object.
Another method to list all the methods of a class is by using the dir() in Python. The dir() returns all the properties and the functions related to a class.
For example:
import random
print("The random library's contents are as follows:")
print(dir(random))
Output :
The random library's contents are as follows:
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST',
'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence',
'_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__', '_acos', '_ceil', '_cos', '_e', '_exp',
'_inst', '_log', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator',
'_urandom', '_warn', 'betavariate', 'choice', 'expovariate', 'gammavariate', 'gauss',
'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint',
'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform',
'vonmisesvariate', 'weibullvariate']
What are list methods?
Python list methods are a list of built-in functions that are used to modify the lists as per the convenience of the user.
How do you list all functions in Python?
The python dir() can be used to list all the functions and attributes of a particular module or class.
Leave a Reply