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. This is
21
the same object as ``list`` and ``types.ListType`` in the Python layer.
24
.. cfunction:: int PyList_Check(PyObject *p)
26
Return true if *p* is a list object or an instance of a subtype of the list
29
.. versionchanged:: 2.2
30
Allowed subtypes to be accepted.
33
.. cfunction:: int PyList_CheckExact(PyObject *p)
35
Return true if *p* is a list object, but not an instance of a subtype of the
41
.. cfunction:: PyObject* PyList_New(Py_ssize_t len)
43
Return a new list of length *len* on success, or *NULL* on failure.
47
If *length* is greater than zero, the returned list object's items are set to
48
``NULL``. Thus you cannot use abstract API functions such as
49
:cfunc:`PySequence_SetItem` or expose the object to Python code before setting
50
all items to a real object with :cfunc:`PyList_SetItem`.
53
.. cfunction:: Py_ssize_t PyList_Size(PyObject *list)
55
.. index:: builtin: len
57
Return the length of the list object in *list*; this is equivalent to
58
``len(list)`` on a list object.
61
.. cfunction:: Py_ssize_t PyList_GET_SIZE(PyObject *list)
63
Macro form of :cfunc:`PyList_Size` without error checking.
66
.. cfunction:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)
68
Return the object at position *pos* in the list pointed to by *p*. The position
69
must be positive, indexing from the end of the list is not supported. If *pos*
70
is out of bounds, return *NULL* and set an :exc:`IndexError` exception.
73
.. cfunction:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
75
Macro form of :cfunc:`PyList_GetItem` without error checking.
78
.. cfunction:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
80
Set the item at index *index* in list to *item*. Return ``0`` on success or
85
This function "steals" a reference to *item* and discards a reference to an item
86
already in the list at the affected position.
89
.. cfunction:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
91
Macro form of :cfunc:`PyList_SetItem` without error checking. This is normally
92
only used to fill in new lists where there is no previous content.
96
This function "steals" a reference to *item*, and, unlike
97
:cfunc:`PyList_SetItem`, does *not* discard a reference to any item that it
98
being replaced; any reference in *list* at position *i* will be leaked.
101
.. cfunction:: int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
103
Insert the item *item* into list *list* in front of index *index*. Return ``0``
104
if successful; return ``-1`` and set an exception if unsuccessful. Analogous to
105
``list.insert(index, item)``.
108
.. cfunction:: int PyList_Append(PyObject *list, PyObject *item)
110
Append the object *item* at the end of list *list*. Return ``0`` if successful;
111
return ``-1`` and set an exception if unsuccessful. Analogous to
112
``list.append(item)``.
115
.. cfunction:: PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
117
Return a list of the objects in *list* containing the objects *between* *low*
118
and *high*. Return *NULL* and set an exception if unsuccessful. Analogous to
122
.. cfunction:: int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
124
Set the slice of *list* between *low* and *high* to the contents of *itemlist*.
125
Analogous to ``list[low:high] = itemlist``. The *itemlist* may be *NULL*,
126
indicating the assignment of an empty list (slice deletion). Return ``0`` on
127
success, ``-1`` on failure.
130
.. cfunction:: int PyList_Sort(PyObject *list)
132
Sort the items of *list* in place. Return ``0`` on success, ``-1`` on failure.
133
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