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.
= [1, 2, 3]
my_list 4)
my_list.append(print(my_list) # Output: [1, 2, 3, 4]
insert(index, item)
: Inserts an item at a specific index.
1, 5)
my_list.insert(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.
6, 7])
my_list.extend([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.
2)
my_list.remove(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.
= my_list.pop(0)
removed_item 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.
= [1, 2, 3, 2, 4]
my_list = my_list.index(2)
index_of_2 print(index_of_2) # Output: 1
count(item)
: Returns the number of times an item appears in the list.
= my_list.count(2)
count_of_2 print(count_of_2) # Output: 2
sort()
: 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 5)
my_list_copy.append(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.