In this section we will learn the methods provided by a List object in Python.
List Methods in Python
A list in Python is an object!
Python provided a set of useful methods for list objects that we can use in order to run different types of operations on them.
For example, one of these methods is called append() and using it we can add a new element to the end of a list object! (Using this method will increase the size of the target list as well).
The other useful method is index() method by which we can get the index number of a specific element in the target list.
Let’s see the list of methods provided for list objects in Python
| Method Name | Description |
|---|---|
| append() | Using this method, we can add a new element to the target list object. |
| clear() | Using this method we can clear the entire elements of a list object. |
| count() | Using this method, we can count how many times an element appeared in a list. |
| extend() | Using this method we can add the entire elements of another iterable object (like another list for example) to the end of the list that is invoking the method. |
| index() | Using this method, we can get index number of a specific element in a list. |
| insert() | Using this method, we can insert a new element to a list object as a specific position. |
| pop() | Using this element, we can remove a specific element from a list. This method takes an index number and remove the element in that index from the list. |
| remove() | This method takes an element as its argument and removes the first occurrence of the element from the list. |
| reverse() | Using this method, we can reverse the order of a list. |
| sort() | Using this method, we can sort a list. |
In the next couple of sections, we will dive deep into each of the methods mentioned above.