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 |
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 |
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, |
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, |
Result
['bee', 'moth', 'ant'] ['bee', 'moth'] ['bee', 'moth', 'ant'] ['bee', 'ant'] |
clear() |
Removes all items from the list. Equivalent to del |
Result
['bee', 'moth', 'ant'] [] |
index(x[, start[, end]]) |
Returns the position of the first list item that has a value of The optional arguments |
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.
|
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 |
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 |
Result
[] [] ['bee', 'moth', 'ant'] [['bee', 'moth'], ['ant']] ['b', 'e', 'e'] ['I', 'am', 'a', 'tuple'] ['am', 'I', 'a', 'set'] |
or
|
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 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 If more than one item shares the maximum value, only the first one encountered is returned. |
Result
moth wasp [1, 2, 3, 4, 5] |
or
|
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 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 If more than one item shares the minimum value, only the first one encountered is returned. |
Result
bee ant [1, 2, 3, 4] |
or
|
Represents an immutable sequence of numbers and is commonly used for looping a specific number of times in It can be used along with |
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] |