8 .. index:: object: list
11 .. ctype:: PyListObject
13 This subtype of :ctype:`PyObject` represents a Python list object.
16 .. cvar:: PyTypeObject PyList_Type
18 .. index:: single: ListType (in module types)
20 This instance of :ctype:`PyTypeObject` represents the Python list type.
21 This is the same object as ``list`` and ``types.ListType`` in the Python
25 .. cfunction:: int PyList_Check(PyObject *p)
27 Return true if *p* is a list object or an instance of a subtype of the list
30 .. versionchanged:: 2.2
31 Allowed subtypes to be accepted.
34 .. cfunction:: int PyList_CheckExact(PyObject *p)
36 Return true if *p* is a list object, but not an instance of a subtype of
42 .. cfunction:: PyObject* PyList_New(Py_ssize_t len)
44 Return a new list of length *len* on success, or *NULL* on failure.
48 If *length* is greater than zero, the returned list object's items are
49 set to ``NULL``. Thus you cannot use abstract API functions such as
50 :cfunc:`PySequence_SetItem` or expose the object to Python code before
51 setting all items to a real object with :cfunc:`PyList_SetItem`.
53 .. versionchanged:: 2.5
54 This function used an :ctype:`int` for *size*. This might require
55 changes in your code for properly supporting 64-bit systems.
58 .. cfunction:: Py_ssize_t PyList_Size(PyObject *list)
60 .. index:: builtin: len
62 Return the length of the list object in *list*; this is equivalent to
63 ``len(list)`` on a list object.
65 .. versionchanged:: 2.5
66 This function returned an :ctype:`int`. This might require changes in
67 your code for properly supporting 64-bit systems.
70 .. cfunction:: Py_ssize_t PyList_GET_SIZE(PyObject *list)
72 Macro form of :cfunc:`PyList_Size` without error checking.
74 .. versionchanged:: 2.5
75 This macro returned an :ctype:`int`. This might require changes in your
76 code for properly supporting 64-bit systems.
79 .. cfunction:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)
81 Return the object at position *pos* in the list pointed to by *p*. The
82 position must be positive, indexing from the end of the list is not
83 supported. If *pos* is out of bounds, return *NULL* and set an
84 :exc:`IndexError` exception.
86 .. versionchanged:: 2.5
87 This function used an :ctype:`int` for *index*. This might require
88 changes in your code for properly supporting 64-bit systems.
91 .. cfunction:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
93 Macro form of :cfunc:`PyList_GetItem` without error checking.
95 .. versionchanged:: 2.5
96 This macro used an :ctype:`int` for *i*. This might require changes in
97 your code for properly supporting 64-bit systems.
100 .. cfunction:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
102 Set the item at index *index* in list to *item*. Return ``0`` on success
103 or ``-1`` on failure.
107 This function "steals" a reference to *item* and discards a reference to
108 an item already in the list at the affected position.
110 .. versionchanged:: 2.5
111 This function used an :ctype:`int` for *index*. This might require
112 changes in your code for properly supporting 64-bit systems.
115 .. cfunction:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
117 Macro form of :cfunc:`PyList_SetItem` without error checking. This is
118 normally only used to fill in new lists where there is no previous content.
122 This macro "steals" a reference to *item*, and, unlike
123 :cfunc:`PyList_SetItem`, does *not* discard a reference to any item that
124 it being replaced; any reference in *list* at position *i* will be
127 .. versionchanged:: 2.5
128 This macro used an :ctype:`int` for *i*. This might require
129 changes in your code for properly supporting 64-bit systems.
132 .. cfunction:: int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
134 Insert the item *item* into list *list* in front of index *index*. Return
135 ``0`` if successful; return ``-1`` and set an exception if unsuccessful.
136 Analogous to ``list.insert(index, item)``.
138 .. versionchanged:: 2.5
139 This function used an :ctype:`int` for *index*. This might require
140 changes in your code for properly supporting 64-bit systems.
143 .. cfunction:: int PyList_Append(PyObject *list, PyObject *item)
145 Append the object *item* at the end of list *list*. Return ``0`` if
146 successful; return ``-1`` and set an exception if unsuccessful. Analogous
147 to ``list.append(item)``.
150 .. cfunction:: PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
152 Return a list of the objects in *list* containing the objects *between* *low*
153 and *high*. Return *NULL* and set an exception if unsuccessful. Analogous
154 to ``list[low:high]``. Negative indices, as when slicing from Python, are not
157 .. versionchanged:: 2.5
158 This function used an :ctype:`int` for *low* and *high*. This might
159 require changes in your code for properly supporting 64-bit systems.
162 .. cfunction:: int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
164 Set the slice of *list* between *low* and *high* to the contents of
165 *itemlist*. Analogous to ``list[low:high] = itemlist``. The *itemlist* may
166 be *NULL*, indicating the assignment of an empty list (slice deletion).
167 Return ``0`` on success, ``-1`` on failure. Negative indices, as when
168 slicing from Python, are not supported.
170 .. versionchanged:: 2.5
171 This function used an :ctype:`int` for *low* and *high*. This might
172 require changes in your code for properly supporting 64-bit systems.
175 .. cfunction:: int PyList_Sort(PyObject *list)
177 Sort the items of *list* in place. Return ``0`` on success, ``-1`` on
178 failure. This is equivalent to ``list.sort()``.
181 .. cfunction:: int PyList_Reverse(PyObject *list)
183 Reverse the items of *list* in place. Return ``0`` on success, ``-1`` on
184 failure. This is the equivalent of ``list.reverse()``.
187 .. cfunction:: PyObject* PyList_AsTuple(PyObject *list)
189 .. index:: builtin: tuple
191 Return a new tuple object containing the contents of *list*; equivalent to