blob: 3006b6cff91ec87a260e82dcef3fed72249dcb07 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _dictobjects:
4
5Dictionary Objects
6------------------
7
8.. index:: object: dictionary
9
10
Sandro Tosi98ed08f2012-01-14 16:42:02 +010011.. c:type:: PyDictObject
Georg Brandlf6842722008-01-19 22:08:21 +000012
Sandro Tosi98ed08f2012-01-14 16:42:02 +010013 This subtype of :c:type:`PyObject` represents a Python dictionary object.
Georg Brandlf6842722008-01-19 22:08:21 +000014
15
Sandro Tosi98ed08f2012-01-14 16:42:02 +010016.. c:var:: PyTypeObject PyDict_Type
Georg Brandlf6842722008-01-19 22:08:21 +000017
18 .. index::
19 single: DictType (in module types)
20 single: DictionaryType (in module types)
21
Sandro Tosi98ed08f2012-01-14 16:42:02 +010022 This instance of :c:type:`PyTypeObject` represents the Python dictionary
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +000023 type. This is exposed to Python programs as ``dict`` and
24 ``types.DictType``.
Georg Brandlf6842722008-01-19 22:08:21 +000025
26
Sandro Tosi98ed08f2012-01-14 16:42:02 +010027.. c:function:: int PyDict_Check(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000028
29 Return true if *p* is a dict object or an instance of a subtype of the dict
30 type.
31
32 .. versionchanged:: 2.2
33 Allowed subtypes to be accepted.
34
35
Sandro Tosi98ed08f2012-01-14 16:42:02 +010036.. c:function:: int PyDict_CheckExact(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000037
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +000038 Return true if *p* is a dict object, but not an instance of a subtype of
39 the dict type.
Georg Brandlf6842722008-01-19 22:08:21 +000040
41 .. versionadded:: 2.4
42
43
Sandro Tosi98ed08f2012-01-14 16:42:02 +010044.. c:function:: PyObject* PyDict_New()
Georg Brandlf6842722008-01-19 22:08:21 +000045
46 Return a new empty dictionary, or *NULL* on failure.
47
48
Sandro Tosi98ed08f2012-01-14 16:42:02 +010049.. c:function:: PyObject* PyDictProxy_New(PyObject *dict)
Georg Brandlf6842722008-01-19 22:08:21 +000050
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +000051 Return a proxy object for a mapping which enforces read-only behavior.
52 This is normally used to create a proxy to prevent modification of the
53 dictionary for non-dynamic class types.
Georg Brandlf6842722008-01-19 22:08:21 +000054
55 .. versionadded:: 2.2
56
57
Sandro Tosi98ed08f2012-01-14 16:42:02 +010058.. c:function:: void PyDict_Clear(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000059
60 Empty an existing dictionary of all key-value pairs.
61
62
Sandro Tosi98ed08f2012-01-14 16:42:02 +010063.. c:function:: int PyDict_Contains(PyObject *p, PyObject *key)
Georg Brandlf6842722008-01-19 22:08:21 +000064
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +000065 Determine if dictionary *p* contains *key*. If an item in *p* is matches
66 *key*, return ``1``, otherwise return ``0``. On error, return ``-1``.
67 This is equivalent to the Python expression ``key in p``.
Georg Brandlf6842722008-01-19 22:08:21 +000068
69 .. versionadded:: 2.4
70
71
Sandro Tosi98ed08f2012-01-14 16:42:02 +010072.. c:function:: PyObject* PyDict_Copy(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000073
74 Return a new dictionary that contains the same key-value pairs as *p*.
75
76 .. versionadded:: 1.6
77
78
Sandro Tosi98ed08f2012-01-14 16:42:02 +010079.. c:function:: int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
Georg Brandlf6842722008-01-19 22:08:21 +000080
81 Insert *value* into the dictionary *p* with a key of *key*. *key* must be
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +000082 :term:`hashable`; if it isn't, :exc:`TypeError` will be raised. Return
83 ``0`` on success or ``-1`` on failure.
Georg Brandlf6842722008-01-19 22:08:21 +000084
85
Sandro Tosi98ed08f2012-01-14 16:42:02 +010086.. c:function:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
Georg Brandlf6842722008-01-19 22:08:21 +000087
88 .. index:: single: PyString_FromString()
89
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +000090 Insert *value* into the dictionary *p* using *key* as a key. *key* should
Sandro Tosi98ed08f2012-01-14 16:42:02 +010091 be a :c:type:`char\*`. The key object is created using
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +000092 ``PyString_FromString(key)``. Return ``0`` on success or ``-1`` on
93 failure.
Georg Brandlf6842722008-01-19 22:08:21 +000094
95
Sandro Tosi98ed08f2012-01-14 16:42:02 +010096.. c:function:: int PyDict_DelItem(PyObject *p, PyObject *key)
Georg Brandlf6842722008-01-19 22:08:21 +000097
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +000098 Remove the entry in dictionary *p* with key *key*. *key* must be hashable;
99 if it isn't, :exc:`TypeError` is raised. Return ``0`` on success or ``-1``
100 on failure.
Georg Brandlf6842722008-01-19 22:08:21 +0000101
102
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100103.. c:function:: int PyDict_DelItemString(PyObject *p, char *key)
Georg Brandlf6842722008-01-19 22:08:21 +0000104
105 Remove the entry in dictionary *p* which has a key specified by the string
106 *key*. Return ``0`` on success or ``-1`` on failure.
107
108
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100109.. c:function:: PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
Georg Brandlf6842722008-01-19 22:08:21 +0000110
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +0000111 Return the object from dictionary *p* which has a key *key*. Return *NULL*
112 if the key *key* is not present, but *without* setting an exception.
Georg Brandlf6842722008-01-19 22:08:21 +0000113
114
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100115.. c:function:: PyObject* PyDict_GetItemString(PyObject *p, const char *key)
Georg Brandlf6842722008-01-19 22:08:21 +0000116
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100117 This is the same as :c:func:`PyDict_GetItem`, but *key* is specified as a
118 :c:type:`char\*`, rather than a :c:type:`PyObject\*`.
Georg Brandlf6842722008-01-19 22:08:21 +0000119
120
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100121.. c:function:: PyObject* PyDict_Items(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +0000122
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100123 Return a :c:type:`PyListObject` containing all the items from the
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +0000124 dictionary, as in the dictionary method :meth:`dict.items`.
Georg Brandlf6842722008-01-19 22:08:21 +0000125
126
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100127.. c:function:: PyObject* PyDict_Keys(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +0000128
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100129 Return a :c:type:`PyListObject` containing all the keys from the dictionary,
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +0000130 as in the dictionary method :meth:`dict.keys`.
Georg Brandlf6842722008-01-19 22:08:21 +0000131
132
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100133.. c:function:: PyObject* PyDict_Values(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +0000134
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100135 Return a :c:type:`PyListObject` containing all the values from the
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +0000136 dictionary *p*, as in the dictionary method :meth:`dict.values`.
Georg Brandlf6842722008-01-19 22:08:21 +0000137
138
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100139.. c:function:: Py_ssize_t PyDict_Size(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +0000140
141 .. index:: builtin: len
142
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +0000143 Return the number of items in the dictionary. This is equivalent to
144 ``len(p)`` on a dictionary.
Georg Brandlf6842722008-01-19 22:08:21 +0000145
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000146 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100147 This function returned an :c:type:`int` type. This might require changes
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000148 in your code for properly supporting 64-bit systems.
149
Georg Brandlf6842722008-01-19 22:08:21 +0000150
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100151.. c:function:: int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Georg Brandlf6842722008-01-19 22:08:21 +0000152
Jeroen Ruigrok van der Werven6f1d5432009-04-25 14:28:02 +0000153 Iterate over all key-value pairs in the dictionary *p*. The
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100154 :c:type:`Py_ssize_t` referred to by *ppos* must be initialized to ``0``
Jeroen Ruigrok van der Werven6f1d5432009-04-25 14:28:02 +0000155 prior to the first call to this function to start the iteration; the
156 function returns true for each pair in the dictionary, and false once all
157 pairs have been reported. The parameters *pkey* and *pvalue* should either
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100158 point to :c:type:`PyObject\*` variables that will be filled in with each key
Jeroen Ruigrok van der Werven6f1d5432009-04-25 14:28:02 +0000159 and value, respectively, or may be *NULL*. Any references returned through
160 them are borrowed. *ppos* should not be altered during iteration. Its
161 value represents offsets within the internal dictionary structure, and
162 since the structure is sparse, the offsets are not consecutive.
Georg Brandlf6842722008-01-19 22:08:21 +0000163
164 For example::
165
166 PyObject *key, *value;
167 Py_ssize_t pos = 0;
168
169 while (PyDict_Next(self->dict, &pos, &key, &value)) {
170 /* do something interesting with the values... */
171 ...
172 }
173
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +0000174 The dictionary *p* should not be mutated during iteration. It is safe
175 (since Python 2.1) to modify the values of the keys as you iterate over the
176 dictionary, but only so long as the set of keys does not change. For
177 example::
Georg Brandlf6842722008-01-19 22:08:21 +0000178
179 PyObject *key, *value;
180 Py_ssize_t pos = 0;
181
182 while (PyDict_Next(self->dict, &pos, &key, &value)) {
183 int i = PyInt_AS_LONG(value) + 1;
184 PyObject *o = PyInt_FromLong(i);
185 if (o == NULL)
186 return -1;
187 if (PyDict_SetItem(self->dict, key, o) < 0) {
188 Py_DECREF(o);
189 return -1;
190 }
191 Py_DECREF(o);
192 }
193
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000194 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100195 This function used an :c:type:`int *` type for *ppos*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000196 changes in your code for properly supporting 64-bit systems.
197
Georg Brandlf6842722008-01-19 22:08:21 +0000198
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100199.. c:function:: int PyDict_Merge(PyObject *a, PyObject *b, int override)
Georg Brandlf6842722008-01-19 22:08:21 +0000200
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +0000201 Iterate over mapping object *b* adding key-value pairs to dictionary *a*.
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100202 *b* may be a dictionary, or any object supporting :c:func:`PyMapping_Keys`
203 and :c:func:`PyObject_GetItem`. If *override* is true, existing pairs in *a*
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +0000204 will be replaced if a matching key is found in *b*, otherwise pairs will
205 only be added if there is not a matching key in *a*. Return ``0`` on
206 success or ``-1`` if an exception was raised.
Georg Brandlf6842722008-01-19 22:08:21 +0000207
208 .. versionadded:: 2.2
209
210
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100211.. c:function:: int PyDict_Update(PyObject *a, PyObject *b)
Georg Brandlf6842722008-01-19 22:08:21 +0000212
Georg Brandlfbd2db52014-03-25 09:34:30 +0100213 This is the same as ``PyDict_Merge(a, b, 1)`` in C, and is similar to
214 ``a.update(b)`` in Python except that :c:func:`PyDict_Update` doesn't fall
215 back to the iterating over a sequence of key value pairs if the second
216 argument has no "keys" attribute. Return ``0`` on success or ``-1`` if an
217 exception was raised.
Georg Brandlf6842722008-01-19 22:08:21 +0000218
219 .. versionadded:: 2.2
220
221
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100222.. c:function:: int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)
Georg Brandlf6842722008-01-19 22:08:21 +0000223
Jeroen Ruigrok van der Wervenc42c0992009-04-25 14:24:30 +0000224 Update or merge into dictionary *a*, from the key-value pairs in *seq2*.
225 *seq2* must be an iterable object producing iterable objects of length 2,
226 viewed as key-value pairs. In case of duplicate keys, the last wins if
227 *override* is true, else the first wins. Return ``0`` on success or ``-1``
228 if an exception was raised. Equivalent Python (except for the return
229 value)::
Georg Brandlf6842722008-01-19 22:08:21 +0000230
231 def PyDict_MergeFromSeq2(a, seq2, override):
232 for key, value in seq2:
233 if override or key not in a:
234 a[key] = value
235
236 .. versionadded:: 2.2