List Methods
List Methods
We have discussed methods like append(), clear(), extend(), insert(), pop(), remove() before. Now we will learn about some more list methods:
sort(): This method sorts the list in ascending order.
Example 1:
Output:
What if you want to print the list in descending order?
We must give reverse=True as a parameter in the sort method.
Example:
Output:
The reverse parameter is set to False by default.
Note: Do not mistake the reverse parameter with the reverse method.
reverse(): This method reverses the order of the list.
Example:
Output:
index(): This method returns the index of the first occurrence of the list item.
Example:
Output:
count(): Returns the count of the number of items with the given value.
Example:
Output:
copy(): Returns copy of the list. This can be done to perform operations on the list without modifying the original list.
Example:
Output: