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
31 .. cfunction:: int PyList_CheckExact(PyObject *p)
33 Return true if *p* is a list object, but not an instance of a subtype of
37 .. cfunction:: PyObject* PyList_New(Py_ssize_t len)
39 Return a new list of length *len* on success, or *NULL* on failure.
43 If *length* is greater than zero, the returned list object's items are
44 set to ``NULL``. Thus you cannot use abstract API functions such as
45 :cfunc:`PySequence_SetItem` or expose the object to Python code before
46 setting all items to a real object with :cfunc:`PyList_SetItem`.
49 .. cfunction:: Py_ssize_t PyList_Size(PyObject *list)
51 .. index:: builtin: len
53 Return the length of the list object in *list*; this is equivalent to
54 ``len(list)`` on a list object.
57 .. cfunction:: Py_ssize_t PyList_GET_SIZE(PyObject *list)
59 Macro form of :cfunc:`PyList_Size` without error checking.
62 .. cfunction:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)
64 Return the object at position *pos* in the list pointed to by *p*. The
65 position must be positive, indexing from the end of the list is not
66 supported. If *pos* is out of bounds, return *NULL* and set an
67 :exc:`IndexError` exception.
70 .. cfunction:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
72 Macro form of :cfunc:`PyList_GetItem` without error checking.
75 .. cfunction:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
77 Set the item at index *index* in list to *item*. Return ``0`` on success
82 This function "steals" a reference to *item* and discards a reference to
83 an item already in the list at the affected position.
86 .. cfunction:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
88 Macro form of :cfunc:`PyList_SetItem` without error checking. This is
89 normally only used to fill in new lists where there is no previous content.
93 This macro "steals" a reference to *item*, and, unlike
94 :cfunc:`PyList_SetItem`, does *not* discard a reference to any item that
95 is being replaced; any reference in *list* at position *i* will be
99 .. cfunction:: int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
101 Insert the item *item* into list *list* in front of index *index*. Return
102 ``0`` if successful; return ``-1`` and set an exception if unsuccessful.
103 Analogous to ``list.insert(index, item)``.
106 .. cfunction:: int PyList_Append(PyObject *list, PyObject *item)
108 Append the object *item* at the end of list *list*. Return ``0`` if
109 successful; return ``-1`` and set an exception if unsuccessful. Analogous
110 to ``list.append(item)``.
113 .. cfunction:: PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
115 Return a list of the objects in *list* containing the objects *between* *low*
116 and *high*. Return *NULL* and set an exception if unsuccessful. Analogous
117 to ``list[low:high]``. Negative indices, as when slicing from Python, are not
121 .. cfunction:: int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
123 Set the slice of *list* between *low* and *high* to the contents of
124 *itemlist*. Analogous to ``list[low:high] = itemlist``. The *itemlist* may
125 be *NULL*, indicating the assignment of an empty list (slice deletion).
126 Return ``0`` on success, ``-1`` on failure. Negative indices, as when
127 slicing from Python, are not supported.
130 .. cfunction:: int PyList_Sort(PyObject *list)
132 Sort the items of *list* in place. Return ``0`` on success, ``-1`` on
133 failure. This is equivalent to ``list.sort()``.
136 .. cfunction:: int PyList_Reverse(PyObject *list)
138 Reverse the items of *list* in place. Return ``0`` on success, ``-1`` on
139 failure. This is the equivalent of ``list.reverse()``.
142 .. cfunction:: PyObject* PyList_AsTuple(PyObject *list)
144 .. index:: builtin: tuple
146 Return a new tuple object containing the contents of *list*; equivalent to