SwadhyAI Notes

All Python notes

Python List Methods 📋

List Methods Reference

Python lists are mutable sequences. These methods modify or query the list in place.

✨ Most Commonly Used

python
# append() / extend() / insert()
fruits = ["apple", "banana"]
fruits.append("orange")  # ["apple", "banana", "orange"]
fruits.extend(["grape", "kiwi"])  # Adds multiple items
fruits.insert(1, "mango")  # Insert at index 1
python
# remove() / pop() / sort()
numbers = [3, 1, 4, 1, 5]
numbers.remove(1)  # Removes first occurrence
numbers.pop()  # Removes and returns last item
numbers.sort()  # Sorts in place: [1, 3, 4]

📋 Complete Method Reference

MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a shallow copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position (or last if no index given)
remove()Removes the first item with the specified value
reverse()Reverses the order of the list (modifies in place)
sort()Sorts the list (modifies in place)

⚠️ Watch Out!

sort() and reverse() modify the list in place and return None. If you want a sorted copy, use sorted(mylist) instead.