blob: b7225faf408ca0abed07d4f5445c7b916944b0d1 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _dictobjects:
4
5Dictionary Objects
6------------------
7
8.. index:: object: dictionary
9
10
Georg Brandl60203b42010-10-06 10:11:56 +000011.. c:type:: PyDictObject
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 dictionary object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
15
Georg Brandl60203b42010-10-06 10:11:56 +000016.. c:var:: PyTypeObject PyDict_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
Georg Brandl60203b42010-10-06 10:11:56 +000018 This instance of :c:type:`PyTypeObject` represents the Python dictionary
Georg Brandl2aff3352010-10-17 10:59:41 +000019 type. This is the same object as :class:`dict` in the Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000020
21
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:function:: int PyDict_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
24 Return true if *p* is a dict object or an instance of a subtype of the dict
25 type.
26
27
Georg Brandl60203b42010-10-06 10:11:56 +000028.. c:function:: int PyDict_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000029
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000030 Return true if *p* is a dict object, but not an instance of a subtype of
31 the dict type.
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
33
Georg Brandl60203b42010-10-06 10:11:56 +000034.. c:function:: PyObject* PyDict_New()
Georg Brandl54a3faa2008-01-20 09:30:57 +000035
36 Return a new empty dictionary, or *NULL* on failure.
37
38
Victor Stinner0db176f2012-04-16 00:16:30 +020039.. c:function:: PyObject* PyDictProxy_New(PyObject *mapping)
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
Victor Stinner0db176f2012-04-16 00:16:30 +020041 Return a :class:`types.MappingProxyType` object for a mapping which
42 enforces read-only behavior. This is normally used to create a view to
43 prevent modification of the dictionary for non-dynamic class types.
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
45
Georg Brandl60203b42010-10-06 10:11:56 +000046.. c:function:: void PyDict_Clear(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000047
48 Empty an existing dictionary of all key-value pairs.
49
50
Georg Brandl60203b42010-10-06 10:11:56 +000051.. c:function:: int PyDict_Contains(PyObject *p, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000053 Determine if dictionary *p* contains *key*. If an item in *p* is matches
54 *key*, return ``1``, otherwise return ``0``. On error, return ``-1``.
55 This is equivalent to the Python expression ``key in p``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000056
57
Georg Brandl60203b42010-10-06 10:11:56 +000058.. c:function:: PyObject* PyDict_Copy(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000059
60 Return a new dictionary that contains the same key-value pairs as *p*.
61
62
Georg Brandl60203b42010-10-06 10:11:56 +000063.. c:function:: int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
65 Insert *value* into the dictionary *p* with a key of *key*. *key* must be
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000066 :term:`hashable`; if it isn't, :exc:`TypeError` will be raised. Return
67 ``0`` on success or ``-1`` on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000068
69
Georg Brandl60203b42010-10-06 10:11:56 +000070.. c:function:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
Benjamin Peterson87d98bc2009-03-23 21:52:09 +000072 .. index:: single: PyUnicode_FromString()
Georg Brandl54a3faa2008-01-20 09:30:57 +000073
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000074 Insert *value* into the dictionary *p* using *key* as a key. *key* should
Serhiy Storchaka84b8e922017-03-30 10:01:03 +030075 be a :c:type:`const char\*`. The key object is created using
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000076 ``PyUnicode_FromString(key)``. Return ``0`` on success or ``-1`` on
Benjamin Peterson87d98bc2009-03-23 21:52:09 +000077 failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
79
Georg Brandl60203b42010-10-06 10:11:56 +000080.. c:function:: int PyDict_DelItem(PyObject *p, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000082 Remove the entry in dictionary *p* with key *key*. *key* must be hashable;
83 if it isn't, :exc:`TypeError` is raised. Return ``0`` on success or ``-1``
84 on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000085
86
Serhiy Storchakac6792272013-10-19 21:03:34 +030087.. c:function:: int PyDict_DelItemString(PyObject *p, const char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000088
89 Remove the entry in dictionary *p* which has a key specified by the string
90 *key*. Return ``0`` on success or ``-1`` on failure.
91
92
Georg Brandl60203b42010-10-06 10:11:56 +000093.. c:function:: PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000094
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000095 Return the object from dictionary *p* which has a key *key*. Return *NULL*
96 if the key *key* is not present, but *without* setting an exception.
Georg Brandl54a3faa2008-01-20 09:30:57 +000097
Alexandre Vassalottic3e36af2008-06-01 04:16:28 +000098
Georg Brandl60203b42010-10-06 10:11:56 +000099.. c:function:: PyObject* PyDict_GetItemWithError(PyObject *p, PyObject *key)
Alexandre Vassalotti787f3072008-06-01 04:00:18 +0000100
Georg Brandl60203b42010-10-06 10:11:56 +0000101 Variant of :c:func:`PyDict_GetItem` that does not suppress
Alexandre Vassalotti787f3072008-06-01 04:00:18 +0000102 exceptions. Return *NULL* **with** an exception set if an exception
103 occurred. Return *NULL* **without** an exception set if the key
104 wasn't present.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000105
Alexandre Vassalottic3e36af2008-06-01 04:16:28 +0000106
Georg Brandl60203b42010-10-06 10:11:56 +0000107.. c:function:: PyObject* PyDict_GetItemString(PyObject *p, const char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000108
Georg Brandl60203b42010-10-06 10:11:56 +0000109 This is the same as :c:func:`PyDict_GetItem`, but *key* is specified as a
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300110 :c:type:`const char\*`, rather than a :c:type:`PyObject\*`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000111
112
Benjamin Peterson00e98862013-03-07 22:16:29 -0500113.. c:function:: PyObject* PyDict_SetDefault(PyObject *p, PyObject *key, PyObject *default)
114
Ezio Melottifc5c5322013-03-10 20:57:16 +0200115 This is the same as the Python-level :meth:`dict.setdefault`. If present, it
Benjamin Peterson00e98862013-03-07 22:16:29 -0500116 returns the value corresponding to *key* from the dictionary *p*. If the key
Benjamin Peterson37474f42013-03-11 12:17:19 -0500117 is not in the dict, it is inserted with value *defaultobj* and *defaultobj*
118 is returned. This function evaluates the hash function of *key* only once,
Benjamin Peterson28558142013-03-11 11:50:21 -0500119 instead of evaluating it independently for the lookup and the insertion.
Benjamin Peterson00e98862013-03-07 22:16:29 -0500120
Berker Peksage75ffa92016-07-05 17:08:29 +0300121 .. versionadded:: 3.4
Benjamin Peterson00e98862013-03-07 22:16:29 -0500122
Georg Brandl60203b42010-10-06 10:11:56 +0000123.. c:function:: PyObject* PyDict_Items(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000124
Benjamin Peterson3c6830c2010-11-03 21:35:28 +0000125 Return a :c:type:`PyListObject` containing all the items from the dictionary.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000126
127
Georg Brandl60203b42010-10-06 10:11:56 +0000128.. c:function:: PyObject* PyDict_Keys(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129
Benjamin Peterson3c6830c2010-11-03 21:35:28 +0000130 Return a :c:type:`PyListObject` containing all the keys from the dictionary.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000131
132
Georg Brandl60203b42010-10-06 10:11:56 +0000133.. c:function:: PyObject* PyDict_Values(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000134
Benjamin Peterson3c6830c2010-11-03 21:35:28 +0000135 Return a :c:type:`PyListObject` containing all the values from the dictionary
136 *p*.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000137
138
Georg Brandl60203b42010-10-06 10:11:56 +0000139.. c:function:: Py_ssize_t PyDict_Size(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140
141 .. index:: builtin: len
142
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000143 Return the number of items in the dictionary. This is equivalent to
144 ``len(p)`` on a dictionary.
145
Georg Brandl54a3faa2008-01-20 09:30:57 +0000146
Georg Brandl60203b42010-10-06 10:11:56 +0000147.. c:function:: int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000148
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000149 Iterate over all key-value pairs in the dictionary *p*. The
Georg Brandl60203b42010-10-06 10:11:56 +0000150 :c:type:`Py_ssize_t` referred to by *ppos* must be initialized to ``0``
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000151 prior to the first call to this function to start the iteration; the
152 function returns true for each pair in the dictionary, and false once all
153 pairs have been reported. The parameters *pkey* and *pvalue* should either
Georg Brandl60203b42010-10-06 10:11:56 +0000154 point to :c:type:`PyObject\*` variables that will be filled in with each key
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000155 and value, respectively, or may be *NULL*. Any references returned through
156 them are borrowed. *ppos* should not be altered during iteration. Its
157 value represents offsets within the internal dictionary structure, and
158 since the structure is sparse, the offsets are not consecutive.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000159
160 For example::
161
162 PyObject *key, *value;
163 Py_ssize_t pos = 0;
164
165 while (PyDict_Next(self->dict, &pos, &key, &value)) {
166 /* do something interesting with the values... */
167 ...
168 }
169
Georg Brandle6bcc912008-05-12 18:05:20 +0000170 The dictionary *p* should not be mutated during iteration. It is safe to
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000171 modify the values of the keys as you iterate over the dictionary, but only
172 so long as the set of keys does not change. For example::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000173
174 PyObject *key, *value;
175 Py_ssize_t pos = 0;
176
177 while (PyDict_Next(self->dict, &pos, &key, &value)) {
178 long i = PyLong_AsLong(value);
179 if (i == -1 && PyErr_Occurred()) {
180 return -1;
181 }
182 PyObject *o = PyLong_FromLong(i + 1);
183 if (o == NULL)
184 return -1;
185 if (PyDict_SetItem(self->dict, key, o) < 0) {
186 Py_DECREF(o);
187 return -1;
188 }
189 Py_DECREF(o);
190 }
191
192
Georg Brandl60203b42010-10-06 10:11:56 +0000193.. c:function:: int PyDict_Merge(PyObject *a, PyObject *b, int override)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000194
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000195 Iterate over mapping object *b* adding key-value pairs to dictionary *a*.
Georg Brandl60203b42010-10-06 10:11:56 +0000196 *b* may be a dictionary, or any object supporting :c:func:`PyMapping_Keys`
197 and :c:func:`PyObject_GetItem`. If *override* is true, existing pairs in *a*
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000198 will be replaced if a matching key is found in *b*, otherwise pairs will
199 only be added if there is not a matching key in *a*. Return ``0`` on
200 success or ``-1`` if an exception was raised.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000201
202
Georg Brandl60203b42010-10-06 10:11:56 +0000203.. c:function:: int PyDict_Update(PyObject *a, PyObject *b)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000204
Georg Brandl1c669c12014-03-25 09:34:30 +0100205 This is the same as ``PyDict_Merge(a, b, 1)`` in C, and is similar to
206 ``a.update(b)`` in Python except that :c:func:`PyDict_Update` doesn't fall
207 back to the iterating over a sequence of key value pairs if the second
208 argument has no "keys" attribute. Return ``0`` on success or ``-1`` if an
209 exception was raised.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000210
211
Georg Brandl60203b42010-10-06 10:11:56 +0000212.. c:function:: int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000213
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000214 Update or merge into dictionary *a*, from the key-value pairs in *seq2*.
215 *seq2* must be an iterable object producing iterable objects of length 2,
216 viewed as key-value pairs. In case of duplicate keys, the last wins if
217 *override* is true, else the first wins. Return ``0`` on success or ``-1``
218 if an exception was raised. Equivalent Python (except for the return
219 value)::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000220
221 def PyDict_MergeFromSeq2(a, seq2, override):
222 for key, value in seq2:
223 if override or key not in a:
224 a[key] = value
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100225
226
227.. c:function:: int PyDict_ClearFreeList()
228
229 Clear the free list. Return the total number of freed items.
230
231 .. versionadded:: 3.3