DEV Community

Cover image for Day 22/100: Useful List Methods in Python (append, pop, sort, and more)
 Rahul Gupta
Rahul Gupta

Posted on

Day 22/100: Useful List Methods in Python (append, pop, sort, and more)

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']
Enter fullscreen mode Exit fullscreen mode

βœ… 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']
Enter fullscreen mode Exit fullscreen mode

βœ… 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']
Enter fullscreen mode Exit fullscreen mode

⚠️ 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'
Enter fullscreen mode Exit fullscreen mode

βœ… Useful when you want to both remove and retrieve an item.


πŸ” 5. clear() – Remove All Items

fruits.clear()
print(fruits)  # []
Enter fullscreen mode Exit fullscreen mode

βœ… 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]
Enter fullscreen mode Exit fullscreen mode

⚠️ Modifies the original list!

For descending order:

numbers.sort(reverse=True)
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ 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]
Enter fullscreen mode Exit fullscreen mode

βœ… 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]
Enter fullscreen mode Exit fullscreen mode

πŸ“Š 9. count(item) – Count Occurrences

names = ["Alice", "Bob", "Alice"]
print(names.count("Alice"))  # 2
Enter fullscreen mode Exit fullscreen mode

πŸ” 10. index(item) – Get Index of First Occurrence

names = ["Alice", "Bob", "Alice"]
print(names.index("Bob"))  # 1
Enter fullscreen mode Exit fullscreen mode

⚠️ 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]
Enter fullscreen mode Exit fullscreen mode

βœ… 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() vs extend(), sort() vs sorted()
  • Best practices for modifying and accessing list elements

Top comments (0)