Python lists come with many built-in methods that let you add, remove, search, copy, and reorder elements easily. Mastering these methods makes your code shorter, cleaner, and easier to read.
In this page, you'll learn the most commonly used list methods such as append(), insert(), remove(), pop(), clear(), copy(), extend(), count(), index(), sort(), and reverse().
append(), insert(), remove(), pop(), clear(), sort(), and reverse() modify the list directly.copy() returns a new list.count() and index() help find elements.extend() adds multiple elements from another iterable.Each list method is called using the dot notation on a list object:
list_name.method_name(arguments)
append(x) → add a single item x at the end.insert(i, x) → insert x at index i.remove(x) → remove the first occurrence of x.pop(i) → remove and return the item at index i (last item if i is not given).clear() → remove all items from the list.copy() → return a shallow copy of the list.extend(iterable) → add all elements from another iterable.count(x) → return how many times x appears.index(x) → return the index of the first occurrence of x.sort() → sort the list in place (ascending by default).reverse() → reverse the elements in place.Adds an item to the end of the list.
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
Inserts an item at a specified position.
fruits = ["apple", "banana"]
fruits.insert(1, "orange")
print(fruits)
Removes the first occurrence of the specified item.
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits)
Removes an item at the given position. If no index is specified, removes the last item.
fruits = ["apple", "banana", "cherry"]
fruits.pop(1)
print(fruits)
Removes all items from the list.
fruits = ["apple", "banana", "cherry"]
fruits.clear()
print(fruits)
Returns a shallow copy of the list.
fruits = ["apple", "banana"]
new_list = fruits.copy()
print(new_list)
Adds elements of another list (or iterable) to the current list.
fruits = ["apple", "banana"]
more = ["cherry", "mango"]
fruits.extend(more)
print(fruits)
Returns the number of times a specified value appears in the list.
numbers = [1, 2, 2, 3, 4, 2]
print(numbers.count(2))
Returns the index of the first occurrence of a specified value.
fruits = ["apple", "banana", "cherry"]
print(fruits.index("banana"))
Sorts the list in ascending order by default.
numbers = [3, 1, 4, 2]
numbers.sort()
print(numbers)
Reverses the order of the list.
numbers = [1, 2, 3, 4]
numbers.reverse()
print(numbers)
append() → ["apple", "banana", "cherry"]insert(1, "orange") → ["apple", "orange", "banana"]remove("banana") → ["apple", "cherry"]pop(1) on ["apple", "banana", "cherry"] removes "banana" and prints ["apple", "cherry"].clear() → the list becomes [].copy() → creates a new list with the same elements as the original.extend(["cherry", "mango"]) → ["apple", "banana", "cherry", "mango"].count(2) in [1, 2, 2, 3, 4, 2] → 3.index("banana") in ["apple", "banana", "cherry"] → 1.sort() on [3, 1, 4, 2] → [1, 2, 3, 4].reverse() on [1, 2, 3, 4] → [4, 3, 2, 1].Try running each example in a Python shell or IDE to see the behavior yourself. Pay attention to which methods return values and which simply modify the list.
copy() or slicing (new_list = old_list[:]) to avoid modifying the original list accidentally.sort() and reverse() modify the list in place and return None.pop() when you need both to remove an element and to use its value.if x in my_list:) before calling remove() or index() to avoid errors.append() and insert() to add more titles.remove() and pop() to delete specific items and display what was removed.extend() to merge two lists, such as fruits and vegetables.count() and index() on a list with duplicate values.sort() and then reverse it with reverse().copy(), modify the copy, and verify that the original list is unchanged.