blob: 4ea71bc2087cffa65b42ff7d9970f1745f1fbb50 [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
18 .. index::
19 single: DictType (in module types)
20 single: DictionaryType (in module types)
21
Georg Brandl60203b42010-10-06 10:11:56 +000022 This instance of :c:type:`PyTypeObject` represents the Python dictionary
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000023 type. This is exposed to Python programs as ``dict`` and
24 ``types.DictType``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
26
Georg Brandl60203b42010-10-06 10:11:56 +000027.. c:function:: int PyDict_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
29 Return true if *p* is a dict object or an instance of a subtype of the dict
30 type.
31
32
Georg Brandl60203b42010-10-06 10:11:56 +000033.. c:function:: int PyDict_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000034
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000035 Return true if *p* is a dict object, but not an instance of a subtype of
36 the dict type.
Georg Brandl54a3faa2008-01-20 09:30:57 +000037
38
Georg Brandl60203b42010-10-06 10:11:56 +000039.. c:function:: PyObject* PyDict_New()
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
41 Return a new empty dictionary, or *NULL* on failure.
42
43
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:function:: PyObject* PyDictProxy_New(PyObject *dict)
Georg Brandl54a3faa2008-01-20 09:30:57 +000045
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000046 Return a proxy object for a mapping which enforces read-only behavior.
47 This is normally used to create a proxy to prevent modification of the
48 dictionary for non-dynamic class types.
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
50
Georg Brandl60203b42010-10-06 10:11:56 +000051.. c:function:: void PyDict_Clear(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
53 Empty an existing dictionary of all key-value pairs.
54
55
Georg Brandl60203b42010-10-06 10:11:56 +000056.. c:function:: int PyDict_Contains(PyObject *p, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000058 Determine if dictionary *p* contains *key*. If an item in *p* is matches
59 *key*, return ``1``, otherwise return ``0``. On error, return ``-1``.
60 This is equivalent to the Python expression ``key in p``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
62
Georg Brandl60203b42010-10-06 10:11:56 +000063.. c:function:: PyObject* PyDict_Copy(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
65 Return a new dictionary that contains the same key-value pairs as *p*.
66
67
Georg Brandl60203b42010-10-06 10:11:56 +000068.. c:function:: int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
70 Insert *value* into the dictionary *p* with a key of *key*. *key* must be
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000071 :term:`hashable`; if it isn't, :exc:`TypeError` will be raised. Return
72 ``0`` on success or ``-1`` on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000073
74
Georg Brandl60203b42010-10-06 10:11:56 +000075.. c:function:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
Benjamin Peterson87d98bc2009-03-23 21:52:09 +000077 .. index:: single: PyUnicode_FromString()
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000079 Insert *value* into the dictionary *p* using *key* as a key. *key* should
Georg Brandl60203b42010-10-06 10:11:56 +000080 be a :c:type:`char\*`. The key object is created using
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000081 ``PyUnicode_FromString(key)``. Return ``0`` on success or ``-1`` on
Benjamin Peterson87d98bc2009-03-23 21:52:09 +000082 failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000083
84
Georg Brandl60203b42010-10-06 10:11:56 +000085.. c:function:: int PyDict_DelItem(PyObject *p, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000087 Remove the entry in dictionary *p* with key *key*. *key* must be hashable;
88 if it isn't, :exc:`TypeError` is raised. Return ``0`` on success or ``-1``
89 on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000090
91
Georg Brandl60203b42010-10-06 10:11:56 +000092.. c:function:: int PyDict_DelItemString(PyObject *p, char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000093
94 Remove the entry in dictionary *p* which has a key specified by the string
95 *key*. Return ``0`` on success or ``-1`` on failure.
96
97
Georg Brandl60203b42010-10-06 10:11:56 +000098.. c:function:: PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +000099
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000100 Return the object from dictionary *p* which has a key *key*. Return *NULL*
101 if the key *key* is not present, but *without* setting an exception.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000102
Alexandre Vassalottic3e36af2008-06-01 04:16:28 +0000103
Georg Brandl60203b42010-10-06 10:11:56 +0000104.. c:function:: PyObject* PyDict_GetItemWithError(PyObject *p, PyObject *key)
Alexandre Vassalotti787f3072008-06-01 04:00:18 +0000105
Georg Brandl60203b42010-10-06 10:11:56 +0000106 Variant of :c:func:`PyDict_GetItem` that does not suppress
Alexandre Vassalotti787f3072008-06-01 04:00:18 +0000107 exceptions. Return *NULL* **with** an exception set if an exception
108 occurred. Return *NULL* **without** an exception set if the key
109 wasn't present.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000110
Alexandre Vassalottic3e36af2008-06-01 04:16:28 +0000111
Georg Brandl60203b42010-10-06 10:11:56 +0000112.. c:function:: PyObject* PyDict_GetItemString(PyObject *p, const char *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000113
Georg Brandl60203b42010-10-06 10:11:56 +0000114 This is the same as :c:func:`PyDict_GetItem`, but *key* is specified as a
115 :c:type:`char\*`, rather than a :c:type:`PyObject\*`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000116
117
Georg Brandl60203b42010-10-06 10:11:56 +0000118.. c:function:: PyObject* PyDict_Items(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000119
Georg Brandl60203b42010-10-06 10:11:56 +0000120 Return a :c:type:`PyListObject` containing all the items from the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000121 dictionary, as in the dictionary method :meth:`dict.items`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000122
123
Georg Brandl60203b42010-10-06 10:11:56 +0000124.. c:function:: PyObject* PyDict_Keys(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000125
Georg Brandl60203b42010-10-06 10:11:56 +0000126 Return a :c:type:`PyListObject` containing all the keys from the dictionary,
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000127 as in the dictionary method :meth:`dict.keys`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000128
129
Georg Brandl60203b42010-10-06 10:11:56 +0000130.. c:function:: PyObject* PyDict_Values(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000131
Georg Brandl60203b42010-10-06 10:11:56 +0000132 Return a :c:type:`PyListObject` containing all the values from the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000133 dictionary *p*, as in the dictionary method :meth:`dict.values`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000134
135
Georg Brandl60203b42010-10-06 10:11:56 +0000136.. c:function:: Py_ssize_t PyDict_Size(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000137
138 .. index:: builtin: len
139
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000140 Return the number of items in the dictionary. This is equivalent to
141 ``len(p)`` on a dictionary.
142
Georg Brandl54a3faa2008-01-20 09:30:57 +0000143
Georg Brandl60203b42010-10-06 10:11:56 +0000144.. c:function:: int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000145
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000146 Iterate over all key-value pairs in the dictionary *p*. The
Georg Brandl60203b42010-10-06 10:11:56 +0000147 :c:type:`Py_ssize_t` referred to by *ppos* must be initialized to ``0``
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000148 prior to the first call to this function to start the iteration; the
149 function returns true for each pair in the dictionary, and false once all
150 pairs have been reported. The parameters *pkey* and *pvalue* should either
Georg Brandl60203b42010-10-06 10:11:56 +0000151 point to :c:type:`PyObject\*` variables that will be filled in with each key
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000152 and value, respectively, or may be *NULL*. Any references returned through
153 them are borrowed. *ppos* should not be altered during iteration. Its
154 value represents offsets within the internal dictionary structure, and
155 since the structure is sparse, the offsets are not consecutive.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000156
157 For example::
158
159 PyObject *key, *value;
160 Py_ssize_t pos = 0;
161
162 while (PyDict_Next(self->dict, &pos, &key, &value)) {
163 /* do something interesting with the values... */
164 ...
165 }
166
Georg Brandle6bcc912008-05-12 18:05:20 +0000167 The dictionary *p* should not be mutated during iteration. It is safe to
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000168 modify the values of the keys as you iterate over the dictionary, but only
169 so long as the set of keys does not change. For example::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000170
171 PyObject *key, *value;
172 Py_ssize_t pos = 0;
173
174 while (PyDict_Next(self->dict, &pos, &key, &value)) {
175 long i = PyLong_AsLong(value);
176 if (i == -1 && PyErr_Occurred()) {
177 return -1;
178 }
179 PyObject *o = PyLong_FromLong(i + 1);
180 if (o == NULL)
181 return -1;
182 if (PyDict_SetItem(self->dict, key, o) < 0) {
183 Py_DECREF(o);
184 return -1;
185 }
186 Py_DECREF(o);
187 }
188
189
Georg Brandl60203b42010-10-06 10:11:56 +0000190.. c:function:: int PyDict_Merge(PyObject *a, PyObject *b, int override)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000191
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000192 Iterate over mapping object *b* adding key-value pairs to dictionary *a*.
Georg Brandl60203b42010-10-06 10:11:56 +0000193 *b* may be a dictionary, or any object supporting :c:func:`PyMapping_Keys`
194 and :c:func:`PyObject_GetItem`. If *override* is true, existing pairs in *a*
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000195 will be replaced if a matching key is found in *b*, otherwise pairs will
196 only be added if there is not a matching key in *a*. Return ``0`` on
197 success or ``-1`` if an exception was raised.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000198
199
Georg Brandl60203b42010-10-06 10:11:56 +0000200.. c:function:: int PyDict_Update(PyObject *a, PyObject *b)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000201
202 This is the same as ``PyDict_Merge(a, b, 1)`` in C, or ``a.update(b)`` in
203 Python. Return ``0`` on success or ``-1`` if an exception was raised.
204
205
Georg Brandl60203b42010-10-06 10:11:56 +0000206.. c:function:: int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000207
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000208 Update or merge into dictionary *a*, from the key-value pairs in *seq2*.
209 *seq2* must be an iterable object producing iterable objects of length 2,
210 viewed as key-value pairs. In case of duplicate keys, the last wins if
211 *override* is true, else the first wins. Return ``0`` on success or ``-1``
212 if an exception was raised. Equivalent Python (except for the return
213 value)::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000214
215 def PyDict_MergeFromSeq2(a, seq2, override):
216 for key, value in seq2:
217 if override or key not in a:
218 a[key] = value