List Methods
-
This lesson explains commonly used Python list methods and how to use them in real programs.
List Methods
A list method is a built-in function used to perform operations on a list in Python.
It helps to add, remove, modify, or manage elements inside a list easily.
1. append() – Add Item at End
Adds a new item to the end of the list.
Daily Life Example
You bought one more item and put it in your shopping basket.
fruits = ["Apple", "Mango"]
fruits.append("Banana")
print(fruits)
2. insert() – Add Item at Specific Position
Adds an item at a specific index position.
Daily Life Example
You want milk to be first in your shopping basket.
items = ["Bread", "Butter"]
items.insert(0, "Milk")
print(items)
3. extend() – Add Multiple Items
Adds multiple items from another list.
Daily Life Example
Your friend adds their groceries into your basket.
list1 = ["Apple", "Mango"]
list2 = ["Milk", "Bread"]
list1.extend(list2)
print(list1)
4. remove() – Remove Specific Item
Removes the first occurrence of a value.
Daily Life Example
You decided not to buy Mango.
fruits = ["Apple", "Mango", "Banana"]
fruits.remove("Mango")
print(fruits)
5. pop() – Remove Using Index
Removes item by position and returns it.
Daily Life Example
You take out the last item from your bag.
nums = [10, 20, 30]
nums.pop()
print(nums)
6. clear() – Remove All Items
Removes all elements from the list.
Daily Life Example
You empty your shopping basket.
items = ["Milk", "Bread"]
items.clear()
print(items)
7. index() – Find Position
Returns the index of a value.
Daily Life Example
Finding where "Mango" is placed in basket.
fruits = ["Apple", "Mango", "Banana"]
print(fruits.index("Mango"))
8. count() – Count Occurrences
Counts how many times a value appears.
Daily Life Example
How many chocolates you bought.
items = ["Choco", "Choco", "Milk"]
print(items.count("Choco"))
9. sort() – Arrange Items
Sorts the list in ascending order.
Daily Life Example
Arranging books alphabetically.
nums = [5, 2, 9, 1]
nums.sort()
print(nums)
10.reverse() – Reverse Order
Reverses the order of elements.
Daily Life Example
Turning a line of students backward.
nums = [1, 2, 3]
nums.reverse()
print(nums)