Debian Codegraph @

Doc/c-api/list.rst

raw
1 .. highlight:: c
2
3 .. _listobjects:
4
5 List Objects
6 ------------
7
8 .. index:: pair: object; list
9
10
11 .. c:type:: PyListObject
12
13 This subtype of :c:type:`PyObject` represents a Python list object.
14
15
16 .. c:var:: PyTypeObject PyList_Type
17
18 This instance of :c:type:`PyTypeObject` represents the Python list type.
19 This is the same object as :class:`list` in the Python layer.
20
21
22 .. c:function:: int PyList_Check(PyObject *p)
23
24 Return true if *p* is a list object or an instance of a subtype of the list
25 type. This function always succeeds.
26
27
28 .. c:function:: int PyList_CheckExact(PyObject *p)
29
30 Return true if *p* is a list object, but not an instance of a subtype of
31 the list type. This function always succeeds.
32
33
34 .. c:function:: PyObject* PyList_New(Py_ssize_t len)
35
36 Return a new list of length *len* on success, or ``NULL`` on failure.
37
38 .. note::
39
40 If *len* is greater than zero, the returned list object's items are
41 set to ``NULL``. Thus you cannot use abstract API functions such as
42 :c:func:`PySequence_SetItem` or expose the object to Python code before
43 setting all items to a real object with :c:func:`PyList_SetItem` or
44 :c:func:`PyList_SET_ITEM()`. The following APIs are safe APIs before
45 the list is fully initialized: :c:func:`PyList_SetItem()` and :c:func:`PyList_SET_ITEM()`.
46
47
48
49 .. c:function:: Py_ssize_t PyList_Size(PyObject *list)
50
51 .. index:: pair: built-in function; len
52
53 Return the length of the list object in *list*; this is equivalent to
54 ``len(list)`` on a list object.
55
56
57 .. c:function:: Py_ssize_t PyList_GET_SIZE(PyObject *list)
58
59 Similar to :c:func:`PyList_Size`, but without error checking.
60
61
62 .. c:function:: PyObject* PyList_GetItemRef(PyObject *list, Py_ssize_t index)
63
64 Return the object at position *index* in the list pointed to by *list*. The
65 position must be non-negative; indexing from the end of the list is not
66 supported. If *index* is out of bounds (:code:`<0 or >=len(list)`),
67 return ``NULL`` and set an :exc:`IndexError` exception.
68
69 .. versionadded:: 3.13
70
71
72 .. c:function:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)
73
74 Like :c:func:`PyList_GetItemRef`, but returns a
75 :term:`borrowed reference` instead of a :term:`strong reference`.
76
77
78 .. c:function:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
79
80 Similar to :c:func:`PyList_GetItem`, but without error checking.
81
82
83 .. c:function:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
84
85 Set the item at index *index* in list to *item*. Return ``0`` on success.
86 If *index* is out of bounds, return ``-1`` and set an :exc:`IndexError`
87 exception.
88
89 .. note::
90
91 This function "steals" a reference to *item* and discards a reference to
92 an item already in the list at the affected position.
93
94
95 .. c:function:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
96
97 Macro form of :c:func:`PyList_SetItem` without error checking. This is
98 normally only used to fill in new lists where there is no previous content.
99
100 Bounds checking is performed as an assertion if Python is built in
101 :ref:`debug mode <debug-build>` or :option:`with assertions
102 <--with-assertions>`.
103
104 .. note::
105
106 This macro "steals" a reference to *item*, and, unlike
107 :c:func:`PyList_SetItem`, does *not* discard a reference to any item that
108 is being replaced; any reference in *list* at position *i* will be
109 leaked.
110
111
112 .. c:function:: int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
113
114 Insert the item *item* into list *list* in front of index *index*. Return
115 ``0`` if successful; return ``-1`` and set an exception if unsuccessful.
116 Analogous to ``list.insert(index, item)``.
117
118
119 .. c:function:: int PyList_Append(PyObject *list, PyObject *item)
120
121 Append the object *item* at the end of list *list*. Return ``0`` if
122 successful; return ``-1`` and set an exception if unsuccessful. Analogous
123 to ``list.append(item)``.
124
125
126 .. c:function:: PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
127
128 Return a list of the objects in *list* containing the objects *between* *low*
129 and *high*. Return ``NULL`` and set an exception if unsuccessful. Analogous
130 to ``list[low:high]``. Indexing from the end of the list is not supported.
131
132
133 .. c:function:: int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
134
135 Set the slice of *list* between *low* and *high* to the contents of
136 *itemlist*. Analogous to ``list[low:high] = itemlist``. The *itemlist* may
137 be ``NULL``, indicating the assignment of an empty list (slice deletion).
138 Return ``0`` on success, ``-1`` on failure. Indexing from the end of the
139 list is not supported.
140
141
142 .. c:function:: int PyList_Extend(PyObject *list, PyObject *iterable)
143
144 Extend *list* with the contents of *iterable*. This is the same as
145 ``PyList_SetSlice(list, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, iterable)``
146 and analogous to ``list.extend(iterable)`` or ``list += iterable``.
147
148 Raise an exception and return ``-1`` if *list* is not a :class:`list`
149 object. Return 0 on success.
150
151 .. versionadded:: 3.13
152
153
154 .. c:function:: int PyList_Clear(PyObject *list)
155
156 Remove all items from *list*. This is the same as
157 ``PyList_SetSlice(list, 0, PY_SSIZE_T_MAX, NULL)`` and analogous to
158 ``list.clear()`` or ``del list[:]``.
159
160 Raise an exception and return ``-1`` if *list* is not a :class:`list`
161 object. Return 0 on success.
162
163 .. versionadded:: 3.13
164
165
166 .. c:function:: int PyList_Sort(PyObject *list)
167
168 Sort the items of *list* in place. Return ``0`` on success, ``-1`` on
169 failure. This is equivalent to ``list.sort()``.
170
171
172 .. c:function:: int PyList_Reverse(PyObject *list)
173
174 Reverse the items of *list* in place. Return ``0`` on success, ``-1`` on
175 failure. This is the equivalent of ``list.reverse()``.
176
177
178 .. c:function:: PyObject* PyList_AsTuple(PyObject *list)
179
180 .. index:: pair: built-in function; tuple
181
182 Return a new tuple object containing the contents of *list*; equivalent to
183 ``tuple(list)``.