List Methods in Python
A list method is a built-in function in Python that we call on a list to perform certain operations. List methods help us add, remove, find, count, sort, reverse and even copy elements.
For better understanding, we have grouped these methods into categories based on what they do.
Categories of List Methods:
- Insertion Methods — to add elements into the list.
- Removal Methods — to remove elements from the list.
- Searching & Counting Methods — to find position and count items.
- Sorting & Reversing Methods — to arrange the order.
- Copying Method — to create a duplicate of the list.
1. Insertion Methods (Adding Elements)
We use these methods to add elements into the list.
append()→ Adds a single element at the end.insert()→ Inserts a single element at a specific index.extend()→ Adds multiple elements from another list or iterable.
Click the next page to explore Insertion methods in detail with examples, outputs and explanations.
2. Removal Methods (Deleting Elements)
These methods help to remove items from the list.
remove()→ Removes first occurrence of the specified value.pop()→ Removes element by index (default is last).clear()→ Removes all elements, making the list empty.
Click the next page to explore Removal methods in detail with examples, outputs and explanations.
3. Searching and Counting Methods
These are used to find position or count frequency of elements in the list.
in→ Used to check if an element exists in the list.not in→ Used to check if an element does not exist in the list.index()→ Returns index of first occurrence of specified value.count()→ Returns number of times a value occurs.
Click the next page to explore Searching and Counting methods in detail with examples, outputs and explanations.
4. Sorting and Reversing Methods
Used to change the order of elements.
sort()→ Sorts the list in ascending or descending order.sort(reverse=True)— Sorts the list in descending order.sorted()— Returns a new sorted list (original remains unchanged).reverse()→ Reverses the order of elements in the list.
Click the next page to explore Sorting & Reversing methods in detail with examples, outputs and explanations.
5. Copying Method
Used to make a shallow copy of the list.
copy()→ Creates a duplicate of the original list.
Click the next page to explore Copying method in detail with examples, outputs and explanations.