Essential List Methods: Adding and Removing Elements
Let’s start with the methods that modify the list itself:
append(item): Adds an item to the end of the list.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]insert(index, item): Inserts an item at a specific index.
my_list.insert(1, 5)
print(my_list) # Output: [1, 5, 2, 3, 4]extend(iterable): Adds all items from an iterable (like another list or tuple) to the end of the list.
my_list.extend([6, 7])
print(my_list) # Output: [1, 5, 2, 3, 4, 6, 7]remove(item): Removes the first occurrence of a specific item. Raises a ValueError if the item is not found.
my_list.remove(2)
print(my_list) # Output: [1, 5, 3, 4, 6, 7]pop([index]): Removes and returns the item at the specified index (defaults to the last item). Raises an IndexError if the index is out of range.
removed_item = my_list.pop(0)
print(removed_item) # Output: 1
print(my_list) # Output: [5, 3, 4, 6, 7]clear(): Removes all items from the list.
my_list.clear()
print(my_list) # Output: []List Methods for Searching and Manipulation
These methods help you find and rearrange elements within your list:
index(item): Returns the index of the first occurrence of an item. Raises a ValueError if the item is not found.
my_list = [1, 2, 3, 2, 4]
index_of_2 = my_list.index(2)
print(index_of_2) # Output: 1count(item): Returns the number of times an item appears in the list.
count_of_2 = my_list.count(2)
print(count_of_2) # Output: 2sort(): Sorts the list in ascending order (in-place). For custom sorting, use the key argument.
my_list.sort()
print(my_list) # Output: [1, 2, 2, 3, 4]reverse(): Reverses the order of items in the list (in-place).
my_list.reverse()
print(my_list) # Output: [4, 3, 2, 2, 1]copy(): Creates a shallow copy of the list. Important for avoiding unintended modifications to the original list.
my_list_copy = my_list.copy()
my_list_copy.append(5)
print(my_list) # Output: [4, 3, 2, 2, 1]
print(my_list_copy) # Output: [4, 3, 2, 2, 1, 5]More Advanced List Operations
These methods provide further control and functionality:
list.copy(): Creates a shallow copy of the list.