Sobes.tech
Senior

Name a few list methods in Python.

sobes.tech AI

Answer from AI

Some commonly used list methods in Python:

  • append(x) — adds element x to the end of the list.
  • extend(iterable) — extends the list by appending elements from the iterable.
  • insert(i, x) — inserts element x at position i.
  • remove(x) — removes the first occurrence of element x.
  • pop([i]) — removes and returns the element at index i (default is the last element).
  • clear() — removes all elements from the list.
  • index(x[, start[, end]]) — returns the index of the first occurrence of x.
  • count(x) — returns the number of occurrences of x.
  • 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]
Name a few list methods in Python. — QA / QA Automation - sobes.tech