Python lists come with many built-in methods that help you add, remove, modify, and manage data easily. These methods make lists one of the most powerful data structures in Python.
In this guide, you will learn all important Python list methods with examples.
🔹 What are List Methods in Python?
List methods are built-in functions that work directly on lists.
They help you:
- Add items
- Remove items
- Sort data
- Copy lists
- Find values
🔹 1. append() Method
The append() method adds an item to the end of the list.
Syntax:
list.append(item)
Example:
fruits = ["apple", "banana"]
fruits.append("mango")
print(fruits)
Output:
['apple', 'banana', 'mango']
🔹 2. insert() Method
The insert() method adds an item at a specific position.
Syntax:
list.insert(index, item)
Example:
numbers = [1, 2, 4]
numbers.insert(2, 3)
print(numbers)
Output:
[1, 2, 3, 4]
🔹 3. extend() Method
The extend() method joins another list into the current list.
Example:
list1 = [1, 2]
list2 = [3, 4]
list1.extend(list2)
print(list1)
Output:
[1, 2, 3, 4]
🔹 4. remove() Method
The remove() method deletes a specific value.
Syntax:
list.remove(value)
Example:
fruits = ["apple", "banana", "mango"]
fruits.remove("banana")
print(fruits)
Output:
['apple', 'mango']
🔹 5. pop() Method
The pop() method removes an item by index (or last item by default).
Example 1: Remove last item
numbers = [10, 20, 30]
numbers.pop()
print(numbers)
Output:
[10, 20]
Example 2: Remove specific index
numbers = [10, 20, 30]
numbers.pop(1)
print(numbers)
Output:
[10, 30]
🔹 6. clear() Method
The clear() method removes all items from the list.
Example:
numbers = [1, 2, 3]
numbers.clear()
print(numbers)
Output:
[]
🔹 7. index() Method
The index() method returns the position of an item.
Example:
fruits = ["apple", "banana", "mango"]
print(fruits.index("mango"))
Output:
2
🔹 8. count() Method
The count() method counts how many times an item appears.
Example:
numbers = [1, 2, 2, 3, 2]
print(numbers.count(2))
Output:
3
🔹 9. sort() Method
The sort() method sorts the list in ascending order.
Example:
numbers = [5, 1, 4, 2, 3]
numbers.sort()
print(numbers)
Output:
[1, 2, 3, 4, 5]
🔹 10. reverse() Method
The reverse() method reverses the list order.
Example:
numbers = [1, 2, 3, 4]
numbers.reverse()
print(numbers)
Output:
[4, 3, 2, 1]
🔹 11. copy() Method
The copy() method creates a new independent list.
Example:
list1 = [1, 2, 3]
list2 = list1.copy()
list2.append(4)
print(list1)
print(list2)
Output:
[1, 2, 3]
[1, 2, 3, 4]
🔹 Summary of List Methods
| Method | Description |
|---|---|
| append() | Add item at end |
| insert() | Add item at index |
| extend() | Join another list |
| remove() | Remove value |
| pop() | Remove by index |
| clear() | Remove all items |
| index() | Find position |
| count() | Count occurrences |
| sort() | Sort list |
| reverse() | Reverse list |
| copy() | Copy list |
🔹 Real-Life Example
Managing a shopping list:
cart = ["milk", "bread", "eggs"]
cart.append("butter")
cart.remove("bread")
cart.sort()
print(cart)
Output:
['butter', 'eggs', 'milk']
🚀 Conclusion
Python list methods are essential tools for handling data efficiently. They allow you to add, remove, sort, search, and manage lists easily.
Mastering these methods will help you build stronger Python projects, especially in data processing and automation.


0 Comments