Python: LISTS (methods & tips)

Rahul S
3 min readSep 9, 2023

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.

--

--