Python list methods
Add, remove, sort and search the items of a list: append, extend, pop, sort and more.
A list is an ordered collection you can change. List methods add items (append, extend, insert), take them out (remove, pop, clear), find them (index, count) and reorder them (sort, reverse). Most of them change the list in place and return None, which is the most common beginner surprise.
Most used
list.append()Adds a single item to the end of a list. The list changes in place and the method itself gives back None.list.extend()Adds every item from another list, tuple, string or other iterable to the end of a list, one by one, in place.list.insert()Puts a new item into a list at the position you choose, moving the items after it one place to the right.list.pop()Removes an item from a list by position and gives it back to you. With no argument it takes the last item.list.remove()Deletes the first item in a list that equals the value you give. Raises ValueError when no item matches.list.sort()Puts the items of a list in order, smallest first by default. It changes the list itself and returns None.
All 11 list methods
- list.append()Adds a single item to the end of a list. The list changes in place and the method itself gives back None.
- list.clear()Empties a list in place, so the same list object ends up with no items. Every variable pointing to it sees the change.
- list.copy()Returns a new list with the same items. Changing the copy's length or items does not touch the original, but nested lists stay shared.
- list.count()Counts how many items in a list are equal to a given value and returns that number. Gives 0 when there are none.
- list.extend()Adds every item from another list, tuple, string or other iterable to the end of a list, one by one, in place.
- list.index()Returns the position of the first item equal to a value. Raises ValueError when the value is not in the list.
- list.insert()Puts a new item into a list at the position you choose, moving the items after it one place to the right.
- list.pop()Removes an item from a list by position and gives it back to you. With no argument it takes the last item.
- list.remove()Deletes the first item in a list that equals the value you give. Raises ValueError when no item matches.
- list.reverse()Turns a list around so the last item comes first. It changes the list itself and returns None; it does not sort.
- list.sort()Puts the items of a list in order, smallest first by default. It changes the list itself and returns None.