list#
list is Python’s mutable, ordered, indexable sequence.
Heterogeneous by default. The default container when the
operator doesn’t yet know which one they want.
Daily kit on list. Examples assume xs = [1, 2, 3].
Operation |
Example |
Effect |
|---|---|---|
Construct from iterable |
|
|
|
|
Add one element to the end (O(1) amortised). |
|
|
Add every element of an iterable to the end. |
|
|
Insert before index (O(n)). |
|
|
Remove and return the last element. |
|
|
Remove and return at index (O(n) at the front). |
|
|
Remove first occurrence by value; |
|
|
In-place sort. |
|
|
In-place reverse. |
|
|
New sorted list. |
|
|
Reverse iterator. |
Index, slice, repeat, concat. Examples assume xs = [1, 2, 3]
and ys = [4, 5].
Form |
Example |
Effect |
|---|---|---|
Index |
|
First element. |
Negative index |
|
Last element. |
Slice |
|
New list with the slice. |
Stride |
|
Every second element. |
Reverse copy |
|
New reversed list. |
Repeat |
|
|
Concat |
|
New list with both sequences. |
Mutating-vs-rebinding bites under augmented assignment. xs +=
ys mutates the list in place; xs = xs + ys rebinds to a
new list.
For the deeper container catalog (deque, Counter,
defaultdict), see
Data Structures.
Method catalog#
The full instance-method surface on list.
Method |
Effect |
|---|---|
|
Add one element at the end (O(1) amortised). |
|
Append every element from an iterable. |
|
Insert before index |
|
Remove and return at index (default last, O(1); O(n) elsewhere). |
|
Remove the first occurrence by value; |
|
Drop every element. |
|
First index of |
|
Number of occurrences. |
|
In-place stable sort (Timsort). |
|
In-place reverse. |
|
Shallow copy (same as |
append adds one element at the end.
xs = [1, 2, 3]
xs.append(4)
xs
[1, 2, 3, 4]
extend appends every element of an iterable.
xs = [1, 2, 3]
xs.extend([4, 5])
xs
[1, 2, 3, 4, 5]
pop with no argument removes and returns the last element.
xs = [1, 2, 3]
xs.pop()
3
sort is in place and stable; pass key for custom orders.
xs = [3, 1, 2]
xs.sort()
xs
[1, 2, 3]
index returns the first index of a value or raises.
[1, 2, 3, 2].index(2)
1
Operator overloading#
list implements the mutable-sequence dunders.
Operator |
Dunder |
Returns |
|---|---|---|
|
|
New list (concatenation). |
|
|
Repeated list. |
|
|
Element-wise equality. |
|
|
Lexicographic less than. |
|
|
Element membership test. |
|
|
Indexing and slicing. |
|
|
In-place element assignment. |
|
|
In-place delete. |
|
|
Length. |
|
|
Iteration. |
|
|
Reverse iterator. |
|
|
In-place |
|
|
In-place repetition. |
|
|
|
A queue class can use __getitem__ / __len__ to behave
as a read-only sequence over an internal deque.
from collections import deque
class Queue:
def __init__(self, items=()): self._q = deque(items)
def push(self, x): self._q.append(x)
def __getitem__(self, i): return self._q[i]
def __len__(self): return len(self._q)
q = Queue([1, 2, 3]); q.push(4)
q[0], q[-1], len(q)
(1, 4, 4)
__add__ and __iadd__ make a wrapper class support both
copy-concat and in-place extend.
class Stack:
def __init__(self, xs=()): self.xs = list(xs)
def __add__(self, other): return Stack(self.xs + other.xs)
def __iadd__(self, other): self.xs.extend(other.xs); return self
def __repr__(self): return f"Stack({self.xs})"
a = Stack([1, 2]); b = Stack([3, 4]); a += b; a
Stack([1, 2, 3, 4])
References#
tuple for the immutable sibling.
Data Structures for
dequeand thecollectionsmodule.Operators for
+,*,in, slicing.