Chapter 6: Data Structures / Lesson 32

List Methods

Introduction to List Methods

List methods are built-in functions that allow you to manipulate lists efficiently. These methods provide powerful ways to add, remove, modify, and organize items in your lists. Understanding list methods is essential for working effectively with lists in Python.

Methods are called using dot notation: list_name.method(). Most list methods modify the list in place, meaning they change the original list rather than creating a new one.

Adding Items

Several methods allow you to add items to a list:

adding_items.py
# append() - adds item to the end fruits = ["apple", "banana"] fruits.append("cherry") print(fruits) # ['apple', 'banana', 'cherry'] # insert() - adds item at specific position fruits.insert(1, "orange") print(fruits) # ['apple', 'orange', 'banana', 'cherry'] # extend() - adds multiple items from another list more_fruits = ["grape", "mango"] fruits.extend(more_fruits) print(fruits) # ['apple', 'orange', 'banana', 'cherry', 'grape', 'mango'] # You can also use += operator fruits += ["kiwi"] print(fruits)

Removing Items

Methods for removing items from lists:

removing_items.py
# remove() - removes first occurrence of value fruits = ["apple", "banana", "cherry", "banana"] fruits.remove("banana") print(fruits) # ['apple', 'cherry', 'banana'] # pop() - removes and returns item at index (default: last item) last = fruits.pop() print(last) # 'banana' print(fruits) # ['apple', 'cherry'] first = fruits.pop(0) print(first) # 'apple' print(fruits) # ['cherry'] # clear() - removes all items fruits.clear() print(fruits) # []

Finding and Counting

Methods to search and count items in lists:

finding_items.py
# index() - returns index of first occurrence fruits = ["apple", "banana", "cherry"] idx = fruits.index("banana") print(idx) # 1 # count() - counts occurrences of value numbers = [1, 2, 2, 3, 2] print(numbers.count(2)) # 3 # Check if item exists before using index() if "orange" in fruits: idx = fruits.index("orange") print(f"Found at index {idx}")

Sorting and Reversing

Methods to organize list items:

sorting.py
# sort() - sorts list in place (ascending by default) numbers = [3, 1, 4, 1, 5] numbers.sort() print(numbers) # [1, 1, 3, 4, 5] # Sort in descending order numbers.sort(reverse=True) print(numbers) # [5, 4, 3, 1, 1] # reverse() - reverses order of items fruits = ["apple", "banana", "cherry"] fruits.reverse() print(fruits) # ['cherry', 'banana', 'apple'] # Note: sorted() is a function that returns new list original = [3, 1, 2] new_list = sorted(original) print(original) # [3, 1, 2] (unchanged) print(new_list) # [1, 2, 3] (sorted)

Copying Lists

Methods to create copies of lists:

copying.py
# copy() - creates shallow copy original = [1, 2, 3] new_list = original.copy() new_list.append(4) print(original) # [1, 2, 3] (unchanged) print(new_list) # [1, 2, 3, 4] # Slicing also creates a copy another_copy = original[:] another_copy.append(5) print(original) # [1, 2, 3] print(another_copy) # [1, 2, 3, 5] # Be careful: assignment creates reference, not copy reference = original reference.append(6) print(original) # [1, 2, 3, 6] (changed!) print(reference) # [1, 2, 3, 6]

Common List Methods Summary

  • append(item) - Add item to end
  • insert(index, item) - Insert item at position
  • extend(iterable) - Add multiple items
  • remove(item) - Remove first occurrence
  • pop([index]) - Remove and return item
  • clear() - Remove all items
  • index(item) - Find index of item
  • count(item) - Count occurrences
  • sort([reverse]) - Sort in place
  • reverse() - Reverse order
  • copy() - Create shallow copy

Best Practices

✅ Method Usage Tips

• Most list methods modify the list in place (they don't return a new list)

• Use copy() when you need to modify a list without changing the original

pop() is useful when you need both the item and its removal

• Check if item exists before using index() to avoid errors

sort() modifies the original; use sorted() if you need the original unchanged

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...