Python List Methods
Python List Methods
In Python, a list is a versatile collection that allows you to store multiple items in a single variable. Python lists come with several built-in methods to manipulate their elements. Here's a list of Python list methods:
- append(x): Adds an item x to the end of the list.
- extend(iterable): Adds all the elements of an iterable (like another list) to the end of the list.
- insert(i, x): Inserts item x at a given position i.
- remove(x): Removes the first occurrence of item x from the list.
- pop([i]): Removes and returns the item at the given position i. If no index is specified, it removes and returns the last item.
- clear(): Removes all items from the list.
- index(x[, start[, end]]): Returns the index of the first occurrence of item x within the list, with optional start and end parameters to limit the search.
- count(x): Returns the number of times item x appears in the list.
- sort(reverse=False, key=None): Sorts the items of the list in ascending order by default. You can specify reverse=True for descending order.
- reverse(): Reverses the order of items in the list.
- copy(): Returns a shallow copy of the list.