Python 3 List Methods & Functions

List of list methods and functions available in Python 3.

List Methods

Method Description Examples
append(x)

Adds an item (x) to the end of the list. This is equivalent to a[len(a):] = [x].

Result
['bee', 'moth']
['bee', 'moth', 'ant']
extend(iterable)

Extends the list by appending all the items from the iterable. This allows you to join two lists together. This method is equivalent to a[len(a):] = iterable.

Result
['bee', 'moth']
['bee', 'moth', 'ant', 'fly']
insert(i, x)

Inserts an item at a given position. The first argument is the index of the element before which to insert. For example, a.insert(0, x) inserts at the front of the list.

Result
['bee', 'moth']
['ant', 'bee', 'moth']
['ant', 'bee', 'fly', 'moth']
remove(x)

Removes the first item from the list that has a value of x. Returns an error if there is no such item.

Result
['bee', 'moth', 'ant']
['bee', 'ant']
pop([i])

Removes the item at the given position in the list, and returns it. If no index is specified, pop() removes and returns the last item in the list.

Result
['bee', 'moth', 'ant']
['bee', 'moth']
['bee', 'moth', 'ant']
['bee', 'ant']
clear()

Removes all items from the list. Equivalent to del a[:].

Result
['bee', 'moth', 'ant']
[]
index(x[, start[, end]])

Returns the position of the first list item that has a value of x. Raises a ValueError if there is no such item.

The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.

Result
1
3
count(x)

Returns the number of times x appears in the list.

Result
1
2
0
sort(key=None, reverse=False)

Sorts the items of the list in place. The arguments can be used to customize the operation.

key
Specifies a function of one argument that is used to extract a comparison key from each list element. The default value is None (compares the elements directly).
reverse
Boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.
Result
[1, 2, 3, 4, 5, 6]
[6, 5, 4, 3, 2, 1]
['ant', 'bee', 'moth', 'wasp']
['bee', 'wasp', 'butterfly']
['butterfly', 'wasp', 'bee']
reverse()

Reverses the elements of the list in place.

Result
[1, 4, 2, 5, 6, 3]
['ant', 'moth', 'wasp', 'bee']
copy()

Returns a shallow copy of the list. Equivalent to a[:].

Result
['bee', 'wasp', 'moth', 'ant']
['bee', 'wasp', 'moth', 'ant']
['bee', 'wasp', 'moth']
['bee', 'wasp', 'moth', 'ant']

List Functions

The following Python functions can be used on lists.

Method Description Examples
len(s)

Returns the number of items in the list.

Result
3
list([iterable])

The list() constructor returns a mutable sequence list of elements. The iterable argument is optional. You can provide any sequence or collection (such as a string, list, tuple, set, dictionary, etc). If no argument is supplied, an empty list is returned.

Result
[]
[]
['bee', 'moth', 'ant']
[['bee', 'moth'], ['ant']]
['b', 'e', 'e']
['I', 'am', 'a', 'tuple']
['am', 'I', 'a', 'set']

max(iterable, *[, key, default])

or

max(arg1, arg2, *args[, key])

Returns the largest item in an iterable (eg, list) or the largest of two or more arguments.

The key argument specifies a one-argument ordering function like that used for sort().

The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.

If more than one item shares the maximum value, only the first one encountered is returned.

Result
moth
wasp
[1, 2, 3, 4, 5]

min(iterable, *[, key, default])

or

min(arg1, arg2, *args[, key])

Returns the smallest item in an iterable (eg, list) or the smallest of two or more arguments.

The key argument specifies a one-argument ordering function like that used for sort().

The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.

If more than one item shares the minimum value, only the first one encountered is returned.

Result
bee
ant
[1, 2, 3, 4]

range(stop)

or

range(start, stop[, step])

Represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

It can be used along with list() to return a list of items between a given range.

Result
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[51, 52, 53, 54, 55]
[1, 3, 5, 7, 9]