On this page
list
Prev: license | Up: __builtins__ | Next: locals
Rel: bool, int, float, complex, str
Ref: docs.python.org — list
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import list
Overview & Signature
list(iterable=(), /)
Built-in mutable sequence.
Common Methods & Attributes
list.append(self, object, /)— Append object to the end of the list.list.clear(self, /)— Remove all items from list.list.copy(self, /)— Return a shallow copy of the list.list.count(self, value, /)— Return number of occurrences of value.list.extend(self, iterable, /)— Extend list by appending elements from the iterable.list.index(self, value, start=0, stop=9223372036854775807, /)— Return first index of value.list.insert(self, index, object, /)— Insert object before index.list.pop(self, index=-1, /)— Remove and return item at index (default last).list.remove(self, value, /)— Remove first occurrence of value.list.reverse(self, /)— Reverse IN PLACE.list.sort(self, /, *, key=None, reverse=False)— Sort the list in ascending order and return None.
Usage
xs = list(range(4))
xs.append(4)
assert xs == [0, 1, 2, 3, 4] and xs[-2:] == [3, 4]
Gotchas
- Don't pop from the front in loops (
xs.pop(0)is $O(n)$): Usecollections.dequefor $O(1)$ queues. - Avoid
[[0] * 3] * 3: That repeats the same inner list 3 times—use[[0] * 3 for _ in range(3)].
REPL Inspect
help(list)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/license Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/locals Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bool Linked here; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; shared tags: #builtins
- tuts/python3/__builtins__/float Linked here; shared tags: #builtins
- tuts/python3/__builtins__/int Linked here; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; shared tags: #builtins
- tuts/python3 links to this page