What Are List Methods?
In Python, list methods are built-in functions attached to list objects that help you manipulate and manage list data. Since lists are mutable, these methods can modify the list in place or help you retrieve information about the list.
Below are the most commonly used list methods with examples.
1. append() - Add an Item to the End
Adds a single element to the end of the list.
fruits = ["apple", "banana"] fruits.append("cherry") print(fruits) # Output: # ['apple', 'banana', 'cherry']2. insert() — Add an Item at a Specific Position
Inserts an element at the given index.
numbers = [1, 2, 4] numbers.insert(2, 3) print(numbers) # Output: # [1, 2, 3, 4]3. extend() — Add Multiple Items
Adds all elements of an iterable (like another list) to the end.
a = [1, 2] b = [3, 4] a.extend(b) print(a) # Output: # [1, 2, 3, 4]4. remove() — Remove First Occurrence of a Value
Deletes the first matching value (not by index).
names = ["Alice", "Bob", "Alice"] names.remove("Alice") print(names) # Output: # ['Bob', 'Alice']If the value is not found, Python raises a ValueError.
5. pop() — Remove by Index
Removes and returns the element at the given index. If no index is specified, it removes the last item.
nums = [10, 20, 30] item = nums.pop(1) print(item) print(nums) # Output: # 20 # [10, 30]6. clear() — Remove All Items
Empties the list completely.
data = [1, 2, 3] data.clear() print(data) # Output: # []7. index() — Find Position of a Value
Returns the index of the first matching value.
letters = ["a", "b", "c"] print(letters.index("b")) # Output: # 1If the value is not in the list, Python raises a ValueError.
8. count() — Count Occurrences of a Value
Returns how many times a value appears in the list.
nums = [1, 2, 2, 3] print(nums.count(2)) # Output: # 29. sort() — Sort the List
Sorts the list in ascending order by default.
nums = [3, 1, 4, 2] nums.sort() print(nums) # Output: # [1, 2, 3, 4]To sort in descending order:
nums.sort(reverse=True) print(nums) # Output: # [4, 3, 2, 1] 10. reverse() — Reverse List Order
Reverses the items in place — no sorting logic, just reverses the current order.
Python nums = [1, 2, 3] nums.reverse() print(nums) # Output: # [3, 2, 1]
nums = [1, 2, 3] nums.reverse() print(nums) # Output: # [3, 2, 1] 11. copy() — Create a Shallow Copy
Returns a shallow copy of the list — similar to list[:].
original = [1, 2, 3] duplicate = original.copy() print(duplicate) # Output: # [1, 2, 3]This is useful when you need a separate list to modify without affecting the original.
12. Working with Sorted Lists Without Modifying Original
To get a sorted list without changing the original, use Python’s built-in sorted() function (not a list method)
values = [3, 1, 2] sorted_values = sorted(values) print(values) print(sorted_values) # Output: # [3, 1, 2] # [1, 2, 3]Important Notes
-
Methods like
append(),insert(),extend(),remove(),pop(),sort(), andreverse()modify the list in place and returnNone. -
sorted()and slicing ([:]) create new lists without modifying the original. -
Using the correct method improves both readability and performance.