Python provides several built in methods to manipulate and manage lists efficiently.
1. append()
The append() method adds a single element at the end of the list.
Syntax
list.append(element)
Example
fruits = ["Apple", "Mango"]
fruits.append("Orange")
print(fruits)
Output
['Apple', 'Mango', 'Orange']
Characteristics
- Adds only one element at a time.
- Modifies the original list.
- Element is added at the end.
2. extend()
The extend() method adds multiple elements from another iterable to the end of the list.
Syntax
list.extend(iterable)
Example
fruits = ["Apple", "Mango"]
fruits.extend(["Orange", "Banana"])
print(fruits)
Output
['Apple', 'Mango', 'Orange', 'Banana']
Characteristics
- Adds multiple elements.
- Extends the existing list.
- Accepts lists, tuples, strings, etc.
3. count()
The count() method returns the number of occurrences of a specified element.
Syntax
list.count(element)
Example
numbers = [10, 20, 10, 30, 10]
print(numbers.count(10))
Output
3
Uses
- Counting repeated values.
- Frequency analysis.
- Data validation.
4. remove()
The remove() method removes the first occurrence of a specified element.
Syntax
list.remove(element)
Example
fruits = ["Apple", "Mango", "Orange"]
fruits.remove("Mango")
print(fruits)
Output
['Apple', 'Orange']
Characteristics
- Removes only the first matching element.
- Generates an error if the element is not found.
5. index()
The index() method returns the position of the first occurrence of an element.
Syntax
list.index(element)
Example
fruits = ["Apple", "Mango", "Orange"]
print(fruits.index("Mango"))
Output
1
Characteristics
- Returns the first matching index.
- Generates an error if the element is absent.
6. pop()
The pop() method removes and returns an element from a specified position.
Syntax
list.pop(index)
Example 1
numbers = [10, 20, 30, 40]
numbers.pop()
print(numbers)
Output
[10, 20, 30]
Example 2
numbers = [10, 20, 30, 40]
numbers.pop(1)
print(numbers)
Output
[10, 30, 40]
Characteristics
- Removes and returns the element.
- By default removes the last element.
7. insert()
The insert() method inserts an element at a specified position.
Syntax
list.insert(index, element)
Example
fruits = ["Apple", "Orange"]
fruits.insert(1, "Mango")
print(fruits)
Output
['Apple', 'Mango', 'Orange']
Characteristics
- Inserts at a given index.
- Existing elements shift to the right.
8. sort()
The sort() method arranges list elements in ascending order by default.
Syntax
list.sort()
Example
numbers = [40, 10, 30, 20]
numbers.sort()
print(numbers)
Output
[10, 20, 30, 40]
Descending Order
numbers = [40, 10, 30, 20]
numbers.sort(reverse=True)
print(numbers)
Output
[40, 30, 20, 10]
Characteristics
- Modifies the original list.
- Sorts numbers and strings.
- Supports ascending and descending order.
9. reverse()
The reverse() method reverses the order of elements in a list.
Syntax
list.reverse()
Example
numbers = [10, 20, 30, 40]
numbers.reverse()
print(numbers)
Output
[40, 30, 20, 10]
Characteristics
- Reverses the list order.
- Changes the original list.
- Different from sorting in descending order.
Summary Table
| Method | Purpose |
|---|---|
| append() | Adds one element at the end |
| extend() | Adds multiple elements |
| count() | Counts occurrences of an element |
| remove() | Removes first occurrence |
| index() | Returns position of element |
| pop() | Removes and returns element |
| insert() | Inserts element at specified position |
| sort() | Sorts list elements |
| reverse() | Reverses list order |
Difference Between append() and extend()
| Basis | append() | extend() |
|---|---|---|
| Elements Added | One element | Multiple elements |
| Argument | Single object | Iterable |
| Result | Adds object as one item | Adds each item separately |
Example
list1 = [1, 2]
list1.append([3, 4])
print(list1)
list2 = [1, 2]
list2.extend([3, 4])
print(list2)
[1, 2, [3, 4]]
[1, 2, 3, 4]
Difference Between remove() and pop()
| Basis | remove() | pop() |
|---|---|---|
| Removes By | Value | Index |
| Return Value | No | Yes |
| Default Action | No default | Removes last element |