| 1 |
.. _list-ops: |
| 2 |
|
| 3 |
Native list operations |
| 4 |
====================== |
| 5 |
|
| 6 |
These ``list`` operations have fast, optimized implementations. Other |
| 7 |
list operations use generic implementations that are often slower. |
| 8 |
|
| 9 |
Construction |
| 10 |
------------ |
| 11 |
|
| 12 |
Construct list with specific items: |
| 13 |
|
| 14 |
* ``[item0, ..., itemN]`` |
| 15 |
|
| 16 |
Construct empty list: |
| 17 |
|
| 18 |
* ``[]`` |
| 19 |
* ``list()`` |
| 20 |
|
| 21 |
Construct list from iterable: |
| 22 |
|
| 23 |
* ``list(x: Iterable)`` |
| 24 |
|
| 25 |
List comprehensions: |
| 26 |
|
| 27 |
* ``[... for ... in ...]`` |
| 28 |
* ``[... for ... in ... if ...]`` |
| 29 |
|
| 30 |
Operators |
| 31 |
--------- |
| 32 |
|
| 33 |
* ``lst[n]`` (get item by integer index) |
| 34 |
* ``lst[n:m]``, ``lst[n:]``, ``lst[:m]``, ``lst[:]`` (slicing) |
| 35 |
* ``lst1 + lst2``, ``lst += iter`` |
| 36 |
* ``lst * n``, ``n * lst``, ``lst *= n`` |
| 37 |
* ``obj in lst`` |
| 38 |
|
| 39 |
Statements |
| 40 |
---------- |
| 41 |
|
| 42 |
Set item by integer index: |
| 43 |
|
| 44 |
* ``lst[n] = x`` |
| 45 |
|
| 46 |
For loop over a list: |
| 47 |
|
| 48 |
* ``for item in lst:`` |
| 49 |
|
| 50 |
Methods |
| 51 |
------- |
| 52 |
|
| 53 |
* ``lst.append(obj)`` |
| 54 |
* ``lst.extend(x: Iterable)`` |
| 55 |
* ``lst.insert(index, obj)`` |
| 56 |
* ``lst.pop(index=-1)`` |
| 57 |
* ``lst.remove(obj)`` |
| 58 |
* ``lst.count(obj)`` |
| 59 |
* ``lst.index(obj)`` |
| 60 |
* ``lst.reverse()`` |
| 61 |
* ``lst.sort()`` |
| 62 |
|
| 63 |
Functions |
| 64 |
--------- |
| 65 |
|
| 66 |
* ``len(lst: list)`` |