Welcome to Day 22 of the 100 Days of Python series!
Yesterday, you learned how to create and manipulate lists. Today, we’ll go deeper into the most commonly used list methods that make lists a truly powerful tool in Python.
If you want to write cleaner, more Pythonic code, mastering these methods is essential. Let’s go! 🐍
📦 What You’ll Learn
- Key list methods:
append()
,insert()
,remove()
,pop()
,sort()
, and more - Real-world examples for each
- When to use which method
- Best practices and common mistakes
🔧 1. append()
– Add Item to End of List
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits) # ['apple', 'banana', 'cherry']
✅ Use when you want to add something to the end of a list.
📍 2. insert(index, item)
– Add at Specific Position
fruits.insert(1, "orange")
print(fruits) # ['apple', 'orange', 'banana', 'cherry']
✅ Use when position matters (e.g., inserting a task at the top of a to-do list).
❌ 3. remove(item)
– Remove by Value
fruits.remove("banana")
print(fruits) # ['apple', 'orange', 'cherry']
⚠️ Raises an error if the item is not in the list.
🧹 4. pop(index)
– Remove by Index (and Get the Item)
last_item = fruits.pop() # Removes last item
print(last_item) # 'cherry'
second_item = fruits.pop(1) # Removes index 1
print(second_item) # 'orange'
✅ Useful when you want to both remove and retrieve an item.
🔁 5. clear()
– Remove All Items
fruits.clear()
print(fruits) # []
✅ Great for resetting a list in-place.
🔄 6. sort()
– Sort List In-Place (Ascending)
numbers = [5, 2, 9, 1]
numbers.sort()
print(numbers) # [1, 2, 5, 9]
⚠️ Modifies the original list!
For descending order:
numbers.sort(reverse=True)
🧪 7. sorted()
– Return a New Sorted List
nums = [4, 1, 7]
new_nums = sorted(nums)
print(new_nums) # [1, 4, 7]
print(nums) # [4, 1, 7]
✅ Use when you want to keep the original list unchanged.
🔁 8. reverse()
– Reverse the List In-Place
nums = [1, 2, 3]
nums.reverse()
print(nums) # [3, 2, 1]
📊 9. count(item)
– Count Occurrences
names = ["Alice", "Bob", "Alice"]
print(names.count("Alice")) # 2
🔍 10. index(item)
– Get Index of First Occurrence
names = ["Alice", "Bob", "Alice"]
print(names.index("Bob")) # 1
⚠️ Raises ValueError
if the item is not found.
➕ 11. extend()
– Merge Two Lists
a = [1, 2]
b = [3, 4]
a.extend(b)
print(a) # [1, 2, 3, 4]
✅ Use instead of +
if you want to add in-place.
💡 Summary Table
Method | Purpose | Modifies Original? |
---|---|---|
append() |
Add to end | ✅ Yes |
insert() |
Add at position | ✅ Yes |
remove() |
Remove by value | ✅ Yes |
pop() |
Remove by index + return | ✅ Yes |
clear() |
Empty the list | ✅ Yes |
sort() |
Sort list in-place | ✅ Yes |
sorted() |
Return sorted copy | ❌ No |
reverse() |
Reverse in-place | ✅ Yes |
count() |
Count value | ❌ No |
index() |
Return index of value | ❌ No |
extend() |
Add another list’s items | ✅ Yes |
🧠 Recap
Today you learned:
- How to use Python’s most common list methods
- When to use
append()
vsextend()
,sort()
vssorted()
- Best practices for modifying and accessing list elements