Senior
Name a few list methods in Python.
sobes.tech AI
Answer from AI
Some commonly used list methods in Python:
append(x)— adds elementxto the end of the list.extend(iterable)— extends the list by appending elements from the iterable.insert(i, x)— inserts elementxat positioni.remove(x)— removes the first occurrence of elementx.pop([i])— removes and returns the element at indexi(default is the last element).clear()— removes all elements from the list.index(x[, start[, end]])— returns the index of the first occurrence ofx.count(x)— returns the number of occurrences ofx.sort(key=None, reverse=False)— sorts the list in place.reverse()— reverses the list in place.
Example:
lst = [3, 1, 4]
lst.append(2) # [3, 1, 4, 2]
lst.sort() # [1, 2, 3, 4]
lst.pop() # 4, list is now [1, 2, 3]