append(x)
Adds element x
to the end of the list.
extend(iterable)
Extends the list by appending elements from the iterable.
insert(index, x)
Inserts element x
at the specified index in the list.
remove(x)
Removes the first occurrence of element x
from the list.
pop([index])
Removes and returns the element at the specified index. If no index is provided, it removes and returns the last element.
clear()
Removes all elements from the list, making it empty.
index(x[, start[, end]])
Returns the index of the first occurrence of element x
in the list within the specified optional start
and end
indexes.
count(x)
Returns the number of occurrences of element x
in the list.
sort(key=None, reverse=False)
Sorts the list in ascending order. Optional key
and reverse
parameters allow custom sorting.
reverse()
Reverses the order of elements in the list.
copy()
Returns a shallow copy of the list. Modifying the copy does not affect the original list.
__getitem__(index)
Allows indexing to access elements in the list. Example: my_list[2]
retrieves the element at index 2.
__setitem__(index, value)
Allows assignment to set elements in the list. Example: my_list[2] = 42
sets the element at index 2 to 42.
__delitem__(index)
Allows deletion of elements using the del
statement. Example: del my_list[2]
deletes the element at index 2.
__len__()
Returns the number of elements in the list using the len()
function.
__iter__()
Provides an iterator for iterating through the elements of the list.
__contains__(x)
Checks if element x
is present in the list. Example: x in my_list
.
__reversed__()
Returns a reverse iterator for iterating through the elements of the list in reverse order.
TIPS & TRICKS
- Checking if an Element Exists in a List: Use the
in
keyword to check if an element exists in a list.
my_list = [1, 2, 3, 4, 5]
if 3 in my_list:
print("3 is in the list")
- Finding the Index of an Element: Use…