blob: feb9015b6daa7287f11406d5f8f591b1e7a4618b [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _listobjects:
4
5List Objects
6------------
7
8.. index:: object: list
9
10
Georg Brandl60203b42010-10-06 10:11:56 +000011.. c:type:: PyListObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000012
Georg Brandl60203b42010-10-06 10:11:56 +000013 This subtype of :c:type:`PyObject` represents a Python list object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
15
Georg Brandl60203b42010-10-06 10:11:56 +000016.. c:var:: PyTypeObject PyList_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
Georg Brandl2aff3352010-10-17 10:59:41 +000018 This instance of :c:type:`PyTypeObject` represents the Python list type.
19 This is the same object as :class:`list` in the Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000020
21
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:function:: int PyList_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
24 Return true if *p* is a list object or an instance of a subtype of the list
25 type.
26
27
Georg Brandl60203b42010-10-06 10:11:56 +000028.. c:function:: int PyList_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000029
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000030 Return true if *p* is a list object, but not an instance of a subtype of
31 the list type.
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
33
Georg Brandl60203b42010-10-06 10:11:56 +000034.. c:function:: PyObject* PyList_New(Py_ssize_t len)
Georg Brandl54a3faa2008-01-20 09:30:57 +000035
36 Return a new list of length *len* on success, or *NULL* on failure.
37
38 .. note::
39
Georg Brandlf7f5a822010-12-01 15:36:33 +000040 If *len* is greater than zero, the returned list object's items are
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000041 set to ``NULL``. Thus you cannot use abstract API functions such as
Georg Brandl60203b42010-10-06 10:11:56 +000042 :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`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
45
Georg Brandl60203b42010-10-06 10:11:56 +000046.. c:function:: Py_ssize_t PyList_Size(PyObject *list)
Georg Brandl54a3faa2008-01-20 09:30:57 +000047
48 .. index:: builtin: len
49
50 Return the length of the list object in *list*; this is equivalent to
51 ``len(list)`` on a list object.
52
53
Georg Brandl60203b42010-10-06 10:11:56 +000054.. c:function:: Py_ssize_t PyList_GET_SIZE(PyObject *list)
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
Georg Brandl60203b42010-10-06 10:11:56 +000056 Macro form of :c:func:`PyList_Size` without error checking.
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
58
Georg Brandl60203b42010-10-06 10:11:56 +000059.. c:function:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)
Georg Brandl54a3faa2008-01-20 09:30:57 +000060
Georg Brandlf7f5a822010-12-01 15:36:33 +000061 Return the object at position *index* in the list pointed to by *list*. The
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000062 position must be positive, indexing from the end of the list is not
Georg Brandlf7f5a822010-12-01 15:36:33 +000063 supported. If *index* is out of bounds, return *NULL* and set an
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000064 :exc:`IndexError` exception.
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
66
Georg Brandl60203b42010-10-06 10:11:56 +000067.. c:function:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
Georg Brandl54a3faa2008-01-20 09:30:57 +000068
Georg Brandl60203b42010-10-06 10:11:56 +000069 Macro form of :c:func:`PyList_GetItem` without error checking.
Georg Brandl54a3faa2008-01-20 09:30:57 +000070
71
Georg Brandl60203b42010-10-06 10:11:56 +000072.. c:function:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
Georg Brandl54a3faa2008-01-20 09:30:57 +000073
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000074 Set the item at index *index* in list to *item*. Return ``0`` on success
75 or ``-1`` on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
77 .. note::
78
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000079 This function "steals" a reference to *item* and discards a reference to
80 an item already in the list at the affected position.
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
82
Georg Brandl60203b42010-10-06 10:11:56 +000083.. c:function:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000084
Georg Brandl60203b42010-10-06 10:11:56 +000085 Macro form of :c:func:`PyList_SetItem` without error checking. This is
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000086 normally only used to fill in new lists where there is no previous content.
Georg Brandl54a3faa2008-01-20 09:30:57 +000087
88 .. note::
89
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000090 This macro "steals" a reference to *item*, and, unlike
Georg Brandl60203b42010-10-06 10:11:56 +000091 :c:func:`PyList_SetItem`, does *not* discard a reference to any item that
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000092 is being replaced; any reference in *list* at position *i* will be
93 leaked.
94
Georg Brandl54a3faa2008-01-20 09:30:57 +000095
Georg Brandl60203b42010-10-06 10:11:56 +000096.. c:function:: int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
Georg Brandl54a3faa2008-01-20 09:30:57 +000097
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000098 Insert the item *item* into list *list* in front of index *index*. Return
99 ``0`` if successful; return ``-1`` and set an exception if unsuccessful.
100 Analogous to ``list.insert(index, item)``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000101
102
Georg Brandl60203b42010-10-06 10:11:56 +0000103.. c:function:: int PyList_Append(PyObject *list, PyObject *item)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000104
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000105 Append the object *item* at the end of list *list*. Return ``0`` if
106 successful; return ``-1`` and set an exception if unsuccessful. Analogous
107 to ``list.append(item)``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000108
109
Georg Brandl60203b42010-10-06 10:11:56 +0000110.. c:function:: PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000111
Georg Brandlc6c31782009-06-08 13:41:29 +0000112 Return a list of the objects in *list* containing the objects *between* *low*
113 and *high*. Return *NULL* and set an exception if unsuccessful. Analogous
114 to ``list[low:high]``. Negative indices, as when slicing from Python, are not
115 supported.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000116
117
Georg Brandl60203b42010-10-06 10:11:56 +0000118.. c:function:: int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000119
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000120 Set the slice of *list* between *low* and *high* to the contents of
121 *itemlist*. Analogous to ``list[low:high] = itemlist``. The *itemlist* may
122 be *NULL*, indicating the assignment of an empty list (slice deletion).
Georg Brandlc6c31782009-06-08 13:41:29 +0000123 Return ``0`` on success, ``-1`` on failure. Negative indices, as when
124 slicing from Python, are not supported.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000125
Georg Brandl54a3faa2008-01-20 09:30:57 +0000126
Georg Brandl60203b42010-10-06 10:11:56 +0000127.. c:function:: int PyList_Sort(PyObject *list)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000128
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000129 Sort the items of *list* in place. Return ``0`` on success, ``-1`` on
130 failure. This is equivalent to ``list.sort()``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000131
132
Georg Brandl60203b42010-10-06 10:11:56 +0000133.. c:function:: int PyList_Reverse(PyObject *list)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000134
135 Reverse the items of *list* in place. Return ``0`` on success, ``-1`` on
136 failure. This is the equivalent of ``list.reverse()``.
137
138
Georg Brandl60203b42010-10-06 10:11:56 +0000139.. c:function:: PyObject* PyList_AsTuple(PyObject *list)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140
141 .. index:: builtin: tuple
142
143 Return a new tuple object containing the contents of *list*; equivalent to
144 ``tuple(list)``.