List Objects¶
- PyTypeObject
PyList_Type¶ - Part of theStable ABI.
This instance of
PyTypeObjectrepresents the Python list type.This is the same object aslistin the Python layer.
- int
PyList_Check(PyObject *p)¶ Return true ifp is a list object or an instance of a subtype of the listtype. This function always succeeds.
- int
PyList_CheckExact(PyObject *p)¶ Return true ifp is a list object, but not an instance of a subtype ofthe list type. This function always succeeds.
- PyObject *
PyList_New(Py_ssize_tlen)¶ - Return value: New reference. Part of theStable ABI.
Return a new list of lengthlen on success, or
NULLon failure.Note
Iflen is greater than zero, the returned list object’s items areset to
NULL. Thus you cannot use abstract API functions such asPySequence_SetItem()or expose the object to Python code beforesetting all items to a real object withPyList_SetItem().
- Py_ssize_t
PyList_Size(PyObject *list)¶ - Part of theStable ABI.
Return the length of the list object inlist; this is equivalent to
len(list)on a list object.
- Py_ssize_t
PyList_GET_SIZE(PyObject *list)¶ Macro form of
PyList_Size()without error checking.
- PyObject *
PyList_GetItem(PyObject *list,Py_ssize_tindex)¶ - Return value: Borrowed reference. Part of theStable ABI.
Return the object at positionindex in the list pointed to bylist. Theposition must be non-negative; indexing from the end of the list is notsupported. Ifindex is out of bounds (<0 or >=len(list)),return
NULLand set anIndexErrorexception.
- PyObject *
PyList_GET_ITEM(PyObject *list,Py_ssize_ti)¶ - Return value: Borrowed reference.
Macro form of
PyList_GetItem()without error checking.
- int
PyList_SetItem(PyObject *list,Py_ssize_tindex,PyObject *item)¶ - Part of theStable ABI.
Set the item at indexindex in list toitem. Return
0on success.Ifindex is out of bounds, return-1and set anIndexErrorexception.Note
This function “steals” a reference toitem and discards a reference toan item already in the list at the affected position.
- void
PyList_SET_ITEM(PyObject *list,Py_ssize_ti,PyObject *o)¶ Macro form of
PyList_SetItem()without error checking. This isnormally only used to fill in new lists where there is no previous content.Note
This macro “steals” a reference toitem, and, unlike
PyList_SetItem(), doesnot discard a reference to any item thatis being replaced; any reference inlist at positioni will beleaked.
- int
PyList_Insert(PyObject *list,Py_ssize_tindex,PyObject *item)¶ - Part of theStable ABI.
Insert the itemitem into listlist in front of indexindex. Return
0if successful; return-1and set an exception if unsuccessful.Analogous tolist.insert(index,item).
- int
PyList_Append(PyObject *list,PyObject *item)¶ - Part of theStable ABI.
Append the objectitem at the end of listlist. Return
0ifsuccessful; return-1and set an exception if unsuccessful. Analogoustolist.append(item).
- PyObject *
PyList_GetSlice(PyObject *list,Py_ssize_tlow,Py_ssize_thigh)¶ - Return value: New reference. Part of theStable ABI.
Return a list of the objects inlist containing the objectsbetweenlowandhigh. Return
NULLand set an exception if unsuccessful. Analogoustolist[low:high]. Indexing from the end of the list is not supported.
- int
PyList_SetSlice(PyObject *list,Py_ssize_tlow,Py_ssize_thigh,PyObject *itemlist)¶ - Part of theStable ABI.
Set the slice oflist betweenlow andhigh to the contents ofitemlist. Analogous to
list[low:high]=itemlist. Theitemlist maybeNULL, indicating the assignment of an empty list (slice deletion).Return0on success,-1on failure. Indexing from the end of thelist is not supported.
- int
PyList_Sort(PyObject *list)¶ - Part of theStable ABI.
Sort the items oflist in place. Return
0on success,-1onfailure. This is equivalent tolist.sort().
- int
PyList_Reverse(PyObject *list)¶ - Part of theStable ABI.
Reverse the items oflist in place. Return
0on success,-1onfailure. This is the equivalent oflist.reverse().
- PyObject *
PyList_AsTuple(PyObject *list)¶ - Return value: New reference. Part of theStable ABI.
Return a new tuple object containing the contents oflist; equivalent to
tuple(list).