Debian Codegraph @

cpython27/Doc/c-api/list.rst

raw
1 .. highlightlang:: c
2
3 .. _listobjects:
4
5 List Objects
6 ------------
7
8 .. index:: object: list
9
10
11 .. c:type:: PyListObject
12
13 This subtype of :c:type:`PyObject` represents a Python list object.
14
15
16 .. c:var:: PyTypeObject PyList_Type
17
18 This instance of :c:type:`PyTypeObject` represents the Python list type. This
19 is the same object as ``list`` in the Python layer.
20
21
22 .. c:function:: int PyList_Check(PyObject *p)
23
24 Return true if *p* is a list object or an instance of a subtype of the list
25 type.
26
27 .. versionchanged:: 2.2
28 Allowed subtypes to be accepted.
29
30
31 .. c:function:: int PyList_CheckExact(PyObject *p)
32
33 Return true if *p* is a list object, but not an instance of a subtype of
34 the list type.
35
36 .. versionadded:: 2.2
37
38
39 .. c:function:: PyObject* PyList_New(Py_ssize_t len)
40
41 Return a new list of length *len* on success, or *NULL* on failure.
42
43 .. note::
44
45 If *len* is greater than zero, the returned list object's items are
46 set to ``NULL``. Thus you cannot use abstract API functions such as
47 :c:func:`PySequence_SetItem` or expose the object to Python code before
48 setting all items to a real object with :c:func:`PyList_SetItem`.
49
50 .. versionchanged:: 2.5
51 This function used an :c:type:`int` for *size*. This might require
52 changes in your code for properly supporting 64-bit systems.
53
54
55 .. c:function:: Py_ssize_t PyList_Size(PyObject *list)
56
57 .. index:: builtin: len
58
59 Return the length of the list object in *list*; this is equivalent to
60 ``len(list)`` on a list object.
61
62 .. versionchanged:: 2.5
63 This function returned an :c:type:`int`. This might require changes in
64 your code for properly supporting 64-bit systems.
65
66
67 .. c:function:: Py_ssize_t PyList_GET_SIZE(PyObject *list)
68
69 Macro form of :c:func:`PyList_Size` without error checking.
70
71 .. versionchanged:: 2.5
72 This macro returned an :c:type:`int`. This might require changes in your
73 code for properly supporting 64-bit systems.
74
75
76 .. c:function:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)
77
78 Return the object at position *index* in the list pointed to by *list*. The
79 position must be non-negative; indexing from the end of the list is not
80 supported. If *index* is out of bounds (<0 or >=len(list)),
81 return *NULL* and set an :exc:`IndexError` exception.
82
83 .. versionchanged:: 2.5
84 This function used an :c:type:`int` for *index*. This might require
85 changes in your code for properly supporting 64-bit systems.
86
87
88 .. c:function:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
89
90 Macro form of :c:func:`PyList_GetItem` without error checking.
91
92 .. versionchanged:: 2.5
93 This macro used an :c:type:`int` for *i*. This might require changes in
94 your code for properly supporting 64-bit systems.
95
96
97 .. c:function:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
98
99 Set the item at index *index* in list to *item*. Return ``0`` on success.
100 If *index* is out of bounds, return ``-1`` and set an :exc:`IndexError`
101 exception.
102
103 .. note::
104
105 This function "steals" a reference to *item* and discards a reference to
106 an item already in the list at the affected position.
107
108 .. versionchanged:: 2.5
109 This function used an :c:type:`int` for *index*. This might require
110 changes in your code for properly supporting 64-bit systems.
111
112
113 .. c:function:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
114
115 Macro form of :c:func:`PyList_SetItem` without error checking. This is
116 normally only used to fill in new lists where there is no previous content.
117
118 .. note::
119
120 This macro "steals" a reference to *item*, and, unlike
121 :c:func:`PyList_SetItem`, does *not* discard a reference to any item that
122 it being replaced; any reference in *list* at position *i* will be
123 leaked.
124
125 .. versionchanged:: 2.5
126 This macro used an :c:type:`int` for *i*. This might require
127 changes in your code for properly supporting 64-bit systems.
128
129
130 .. c:function:: int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
131
132 Insert the item *item* into list *list* in front of index *index*. Return
133 ``0`` if successful; return ``-1`` and set an exception if unsuccessful.
134 Analogous to ``list.insert(index, item)``.
135
136 .. versionchanged:: 2.5
137 This function used an :c:type:`int` for *index*. This might require
138 changes in your code for properly supporting 64-bit systems.
139
140
141 .. c:function:: int PyList_Append(PyObject *list, PyObject *item)
142
143 Append the object *item* at the end of list *list*. Return ``0`` if
144 successful; return ``-1`` and set an exception if unsuccessful. Analogous
145 to ``list.append(item)``.
146
147
148 .. c:function:: PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
149
150 Return a list of the objects in *list* containing the objects *between* *low*
151 and *high*. Return *NULL* and set an exception if unsuccessful. Analogous
152 to ``list[low:high]``. Indexing from the end of the list is not supported.
153
154 .. versionchanged:: 2.5
155 This function used an :c:type:`int` for *low* and *high*. This might
156 require changes in your code for properly supporting 64-bit systems.
157
158
159 .. c:function:: int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
160
161 Set the slice of *list* between *low* and *high* to the contents of
162 *itemlist*. Analogous to ``list[low:high] = itemlist``. The *itemlist* may
163 be *NULL*, indicating the assignment of an empty list (slice deletion).
164 Return ``0`` on success, ``-1`` on failure. Indexing from the end of the
165 list is not supported.
166
167 .. versionchanged:: 2.5
168 This function used an :c:type:`int` for *low* and *high*. This might
169 require changes in your code for properly supporting 64-bit systems.
170
171
172 .. c:function:: int PyList_Sort(PyObject *list)
173
174 Sort the items of *list* in place. Return ``0`` on success, ``-1`` on
175 failure. This is equivalent to ``list.sort()``.
176
177
178 .. c:function:: int PyList_Reverse(PyObject *list)
179
180 Reverse the items of *list* in place. Return ``0`` on success, ``-1`` on
181 failure. This is the equivalent of ``list.reverse()``.
182
183
184 .. c:function:: PyObject* PyList_AsTuple(PyObject *list)
185
186 .. index:: builtin: tuple
187
188 Return a new tuple object containing the contents of *list*; equivalent to
189 ``tuple(list)``.