|
1 .. highlightlang:: c |
|
2 |
|
3 .. _listobjects: |
|
4 |
|
5 List Objects |
|
6 ------------ |
|
7 |
|
8 .. index:: object: list |
|
9 |
|
10 |
|
11 .. ctype:: PyListObject |
|
12 |
|
13 This subtype of :ctype:`PyObject` represents a Python list object. |
|
14 |
|
15 |
|
16 .. cvar:: PyTypeObject PyList_Type |
|
17 |
|
18 .. index:: single: ListType (in module types) |
|
19 |
|
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. |
|
22 |
|
23 |
|
24 .. cfunction:: int PyList_Check(PyObject *p) |
|
25 |
|
26 Return true if *p* is a list object or an instance of a subtype of the list |
|
27 type. |
|
28 |
|
29 .. versionchanged:: 2.2 |
|
30 Allowed subtypes to be accepted. |
|
31 |
|
32 |
|
33 .. cfunction:: int PyList_CheckExact(PyObject *p) |
|
34 |
|
35 Return true if *p* is a list object, but not an instance of a subtype of the |
|
36 list type. |
|
37 |
|
38 .. versionadded:: 2.2 |
|
39 |
|
40 |
|
41 .. cfunction:: PyObject* PyList_New(Py_ssize_t len) |
|
42 |
|
43 Return a new list of length *len* on success, or *NULL* on failure. |
|
44 |
|
45 .. note:: |
|
46 |
|
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`. |
|
51 |
|
52 |
|
53 .. cfunction:: Py_ssize_t PyList_Size(PyObject *list) |
|
54 |
|
55 .. index:: builtin: len |
|
56 |
|
57 Return the length of the list object in *list*; this is equivalent to |
|
58 ``len(list)`` on a list object. |
|
59 |
|
60 |
|
61 .. cfunction:: Py_ssize_t PyList_GET_SIZE(PyObject *list) |
|
62 |
|
63 Macro form of :cfunc:`PyList_Size` without error checking. |
|
64 |
|
65 |
|
66 .. cfunction:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index) |
|
67 |
|
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. |
|
71 |
|
72 |
|
73 .. cfunction:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i) |
|
74 |
|
75 Macro form of :cfunc:`PyList_GetItem` without error checking. |
|
76 |
|
77 |
|
78 .. cfunction:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item) |
|
79 |
|
80 Set the item at index *index* in list to *item*. Return ``0`` on success or |
|
81 ``-1`` on failure. |
|
82 |
|
83 .. note:: |
|
84 |
|
85 This function "steals" a reference to *item* and discards a reference to an item |
|
86 already in the list at the affected position. |
|
87 |
|
88 |
|
89 .. cfunction:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o) |
|
90 |
|
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. |
|
93 |
|
94 .. note:: |
|
95 |
|
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. |
|
99 |
|
100 |
|
101 .. cfunction:: int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item) |
|
102 |
|
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)``. |
|
106 |
|
107 |
|
108 .. cfunction:: int PyList_Append(PyObject *list, PyObject *item) |
|
109 |
|
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)``. |
|
113 |
|
114 |
|
115 .. cfunction:: PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high) |
|
116 |
|
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 |
|
119 ``list[low:high]``. |
|
120 |
|
121 |
|
122 .. cfunction:: int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist) |
|
123 |
|
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. |
|
128 |
|
129 |
|
130 .. cfunction:: int PyList_Sort(PyObject *list) |
|
131 |
|
132 Sort the items of *list* in place. Return ``0`` on success, ``-1`` on failure. |
|
133 This is equivalent to ``list.sort()``. |
|
134 |
|
135 |
|
136 .. cfunction:: int PyList_Reverse(PyObject *list) |
|
137 |
|
138 Reverse the items of *list* in place. Return ``0`` on success, ``-1`` on |
|
139 failure. This is the equivalent of ``list.reverse()``. |
|
140 |
|
141 |
|
142 .. cfunction:: PyObject* PyList_AsTuple(PyObject *list) |
|
143 |
|
144 .. index:: builtin: tuple |
|
145 |
|
146 Return a new tuple object containing the contents of *list*; equivalent to |
|
147 ``tuple(list)``. |