1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>Dictionary Objects — Python 2.7.11 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.11 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
<link rel="up" title="Concrete Objects Layer" href="concrete.html" />
34
<link rel="next" title="Class and Instance Objects" href="class.html" />
35
<link rel="prev" title="List Objects" href="list.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="class.html" title="Class and Instance Objects"
55
accesskey="N">next</a> |</li>
57
<a href="list.html" title="List Objects"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.11</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> »</li>
68
<li class="nav-item nav-item-2"><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="dictionary-objects">
78
<span id="dictobjects"></span><h1>Dictionary Objects<a class="headerlink" href="#dictionary-objects" title="Permalink to this headline">¶</a></h1>
79
<span class="target" id="index-0"></span><dl class="type">
80
<dt id="c.PyDictObject">
81
<code class="descname">PyDictObject</code><a class="headerlink" href="#c.PyDictObject" title="Permalink to this definition">¶</a></dt>
82
<dd><p>This subtype of <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal"><span class="pre">PyObject</span></code></a> represents a Python dictionary object.</p>
86
<dt id="c.PyDict_Type">
87
<a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject">PyTypeObject</a> <code class="descname">PyDict_Type</code><a class="headerlink" href="#c.PyDict_Type" title="Permalink to this definition">¶</a></dt>
88
<dd><p id="index-1">This instance of <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal"><span class="pre">PyTypeObject</span></code></a> represents the Python dictionary
89
type. This is exposed to Python programs as <code class="docutils literal"><span class="pre">dict</span></code> and
90
<code class="docutils literal"><span class="pre">types.DictType</span></code>.</p>
94
<dt id="c.PyDict_Check">
95
int <code class="descname">PyDict_Check</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Check" title="Permalink to this definition">¶</a></dt>
96
<dd><p>Return true if <em>p</em> is a dict object or an instance of a subtype of the dict
98
<div class="versionchanged">
99
<p><span class="versionmodified">Changed in version 2.2: </span>Allowed subtypes to be accepted.</p>
103
<dl class="function">
104
<dt id="c.PyDict_CheckExact">
105
int <code class="descname">PyDict_CheckExact</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_CheckExact" title="Permalink to this definition">¶</a></dt>
106
<dd><p>Return true if <em>p</em> is a dict object, but not an instance of a subtype of
108
<div class="versionadded">
109
<p><span class="versionmodified">New in version 2.4.</span></p>
113
<dl class="function">
114
<dt id="c.PyDict_New">
115
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_New</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_New" title="Permalink to this definition">¶</a></dt>
116
<dd><em class="refcount">Return value: New reference.</em><p>Return a new empty dictionary, or <em>NULL</em> on failure.</p>
119
<dl class="function">
120
<dt id="c.PyDictProxy_New">
121
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDictProxy_New</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *dict</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDictProxy_New" title="Permalink to this definition">¶</a></dt>
122
<dd><em class="refcount">Return value: New reference.</em><p>Return a proxy object for a mapping which enforces read-only behavior.
123
This is normally used to create a proxy to prevent modification of the
124
dictionary for non-dynamic class types.</p>
125
<div class="versionadded">
126
<p><span class="versionmodified">New in version 2.2.</span></p>
130
<dl class="function">
131
<dt id="c.PyDict_Clear">
132
void <code class="descname">PyDict_Clear</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Clear" title="Permalink to this definition">¶</a></dt>
133
<dd><p>Empty an existing dictionary of all key-value pairs.</p>
136
<dl class="function">
137
<dt id="c.PyDict_Contains">
138
int <code class="descname">PyDict_Contains</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Contains" title="Permalink to this definition">¶</a></dt>
139
<dd><p>Determine if dictionary <em>p</em> contains <em>key</em>. If an item in <em>p</em> is matches
140
<em>key</em>, return <code class="docutils literal"><span class="pre">1</span></code>, otherwise return <code class="docutils literal"><span class="pre">0</span></code>. On error, return <code class="docutils literal"><span class="pre">-1</span></code>.
141
This is equivalent to the Python expression <code class="docutils literal"><span class="pre">key</span> <span class="pre">in</span> <span class="pre">p</span></code>.</p>
142
<div class="versionadded">
143
<p><span class="versionmodified">New in version 2.4.</span></p>
147
<dl class="function">
148
<dt id="c.PyDict_Copy">
149
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_Copy</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Copy" title="Permalink to this definition">¶</a></dt>
150
<dd><em class="refcount">Return value: New reference.</em><p>Return a new dictionary that contains the same key-value pairs as <em>p</em>.</p>
151
<div class="versionadded">
152
<p><span class="versionmodified">New in version 1.6.</span></p>
156
<dl class="function">
157
<dt id="c.PyDict_SetItem">
158
int <code class="descname">PyDict_SetItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *val</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_SetItem" title="Permalink to this definition">¶</a></dt>
159
<dd><p>Insert <em>value</em> into the dictionary <em>p</em> with a key of <em>key</em>. <em>key</em> must be
160
<a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>; if it isn’t, <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> will be raised. Return
161
<code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code> on failure.</p>
164
<dl class="function">
165
<dt id="c.PyDict_SetItemString">
166
int <code class="descname">PyDict_SetItemString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, const char<em> *key</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *val</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_SetItemString" title="Permalink to this definition">¶</a></dt>
167
<dd><p id="index-2">Insert <em>value</em> into the dictionary <em>p</em> using <em>key</em> as a key. <em>key</em> should
168
be a <code class="xref c c-type docutils literal"><span class="pre">char*</span></code>. The key object is created using
169
<code class="docutils literal"><span class="pre">PyString_FromString(key)</span></code>. Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code> on
173
<dl class="function">
174
<dt id="c.PyDict_DelItem">
175
int <code class="descname">PyDict_DelItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_DelItem" title="Permalink to this definition">¶</a></dt>
176
<dd><p>Remove the entry in dictionary <em>p</em> with key <em>key</em>. <em>key</em> must be hashable;
177
if it isn’t, <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> is raised. Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code>
181
<dl class="function">
182
<dt id="c.PyDict_DelItemString">
183
int <code class="descname">PyDict_DelItemString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, char<em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_DelItemString" title="Permalink to this definition">¶</a></dt>
184
<dd><p>Remove the entry in dictionary <em>p</em> which has a key specified by the string
185
<em>key</em>. Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code> on failure.</p>
188
<dl class="function">
189
<dt id="c.PyDict_GetItem">
190
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_GetItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_GetItem" title="Permalink to this definition">¶</a></dt>
191
<dd><em class="refcount">Return value: Borrowed reference.</em><p>Return the object from dictionary <em>p</em> which has a key <em>key</em>. Return <em>NULL</em>
192
if the key <em>key</em> is not present, but <em>without</em> setting an exception.</p>
195
<dl class="function">
196
<dt id="c.PyDict_GetItemString">
197
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_GetItemString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, const char<em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_GetItemString" title="Permalink to this definition">¶</a></dt>
198
<dd><em class="refcount">Return value: Borrowed reference.</em><p>This is the same as <a class="reference internal" href="#c.PyDict_GetItem" title="PyDict_GetItem"><code class="xref c c-func docutils literal"><span class="pre">PyDict_GetItem()</span></code></a>, but <em>key</em> is specified as a
199
<code class="xref c c-type docutils literal"><span class="pre">char*</span></code>, rather than a <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal"><span class="pre">PyObject*</span></code></a>.</p>
202
<dl class="function">
203
<dt id="c.PyDict_Items">
204
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_Items</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Items" title="Permalink to this definition">¶</a></dt>
205
<dd><em class="refcount">Return value: New reference.</em><p>Return a <a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal"><span class="pre">PyListObject</span></code></a> containing all the items from the
206
dictionary, as in the dictionary method <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal"><span class="pre">dict.items()</span></code></a>.</p>
209
<dl class="function">
210
<dt id="c.PyDict_Keys">
211
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_Keys</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Keys" title="Permalink to this definition">¶</a></dt>
212
<dd><em class="refcount">Return value: New reference.</em><p>Return a <a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal"><span class="pre">PyListObject</span></code></a> containing all the keys from the dictionary,
213
as in the dictionary method <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal"><span class="pre">dict.keys()</span></code></a>.</p>
216
<dl class="function">
217
<dt id="c.PyDict_Values">
218
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_Values</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Values" title="Permalink to this definition">¶</a></dt>
219
<dd><em class="refcount">Return value: New reference.</em><p>Return a <a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal"><span class="pre">PyListObject</span></code></a> containing all the values from the
220
dictionary <em>p</em>, as in the dictionary method <a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal"><span class="pre">dict.values()</span></code></a>.</p>
223
<dl class="function">
224
<dt id="c.PyDict_Size">
225
Py_ssize_t <code class="descname">PyDict_Size</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Size" title="Permalink to this definition">¶</a></dt>
226
<dd><p id="index-3">Return the number of items in the dictionary. This is equivalent to
227
<code class="docutils literal"><span class="pre">len(p)</span></code> on a dictionary.</p>
228
<div class="versionchanged">
229
<p><span class="versionmodified">Changed in version 2.5: </span>This function returned an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type. This might require changes
230
in your code for properly supporting 64-bit systems.</p>
234
<dl class="function">
235
<dt id="c.PyDict_Next">
236
int <code class="descname">PyDict_Next</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, Py_ssize_t<em> *ppos</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> **pkey</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> **pvalue</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Next" title="Permalink to this definition">¶</a></dt>
237
<dd><p>Iterate over all key-value pairs in the dictionary <em>p</em>. The
238
<code class="xref c c-type docutils literal"><span class="pre">Py_ssize_t</span></code> referred to by <em>ppos</em> must be initialized to <code class="docutils literal"><span class="pre">0</span></code>
239
prior to the first call to this function to start the iteration; the
240
function returns true for each pair in the dictionary, and false once all
241
pairs have been reported. The parameters <em>pkey</em> and <em>pvalue</em> should either
242
point to <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal"><span class="pre">PyObject*</span></code></a> variables that will be filled in with each key
243
and value, respectively, or may be <em>NULL</em>. Any references returned through
244
them are borrowed. <em>ppos</em> should not be altered during iteration. Its
245
value represents offsets within the internal dictionary structure, and
246
since the structure is sparse, the offsets are not consecutive.</p>
248
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">key</span><span class="p">,</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
249
<span class="n">Py_ssize_t</span> <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
251
<span class="k">while</span> <span class="p">(</span><span class="n">PyDict_Next</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">dict</span><span class="p">,</span> <span class="o">&</span><span class="n">pos</span><span class="p">,</span> <span class="o">&</span><span class="n">key</span><span class="p">,</span> <span class="o">&</span><span class="n">value</span><span class="p">))</span> <span class="p">{</span>
252
<span class="cm">/* do something interesting with the values... */</span>
253
<span class="p">...</span>
254
<span class="p">}</span>
257
<p>The dictionary <em>p</em> should not be mutated during iteration. It is safe
258
(since Python 2.1) to modify the values of the keys as you iterate over the
259
dictionary, but only so long as the set of keys does not change. For
261
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">key</span><span class="p">,</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
262
<span class="n">Py_ssize_t</span> <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
264
<span class="k">while</span> <span class="p">(</span><span class="n">PyDict_Next</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">dict</span><span class="p">,</span> <span class="o">&</span><span class="n">pos</span><span class="p">,</span> <span class="o">&</span><span class="n">key</span><span class="p">,</span> <span class="o">&</span><span class="n">value</span><span class="p">))</span> <span class="p">{</span>
265
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">PyInt_AS_LONG</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
266
<span class="n">PyObject</span> <span class="o">*</span><span class="n">o</span> <span class="o">=</span> <span class="n">PyInt_FromLong</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
267
<span class="k">if</span> <span class="p">(</span><span class="n">o</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
268
<span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
269
<span class="k">if</span> <span class="p">(</span><span class="n">PyDict_SetItem</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">dict</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">o</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
270
<span class="n">Py_DECREF</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
271
<span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
272
<span class="p">}</span>
273
<span class="n">Py_DECREF</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
274
<span class="p">}</span>
277
<div class="versionchanged">
278
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span> <span class="pre">*</span></code> type for <em>ppos</em>. This might require
279
changes in your code for properly supporting 64-bit systems.</p>
283
<dl class="function">
284
<dt id="c.PyDict_Merge">
285
int <code class="descname">PyDict_Merge</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *a</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *b</em>, int<em> override</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Merge" title="Permalink to this definition">¶</a></dt>
286
<dd><p>Iterate over mapping object <em>b</em> adding key-value pairs to dictionary <em>a</em>.
287
<em>b</em> may be a dictionary, or any object supporting <a class="reference internal" href="mapping.html#c.PyMapping_Keys" title="PyMapping_Keys"><code class="xref c c-func docutils literal"><span class="pre">PyMapping_Keys()</span></code></a>
288
and <a class="reference internal" href="object.html#c.PyObject_GetItem" title="PyObject_GetItem"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GetItem()</span></code></a>. If <em>override</em> is true, existing pairs in <em>a</em>
289
will be replaced if a matching key is found in <em>b</em>, otherwise pairs will
290
only be added if there is not a matching key in <em>a</em>. Return <code class="docutils literal"><span class="pre">0</span></code> on
291
success or <code class="docutils literal"><span class="pre">-1</span></code> if an exception was raised.</p>
292
<div class="versionadded">
293
<p><span class="versionmodified">New in version 2.2.</span></p>
297
<dl class="function">
298
<dt id="c.PyDict_Update">
299
int <code class="descname">PyDict_Update</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *a</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *b</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Update" title="Permalink to this definition">¶</a></dt>
300
<dd><p>This is the same as <code class="docutils literal"><span class="pre">PyDict_Merge(a,</span> <span class="pre">b,</span> <span class="pre">1)</span></code> in C, and is similar to
301
<code class="docutils literal"><span class="pre">a.update(b)</span></code> in Python except that <a class="reference internal" href="#c.PyDict_Update" title="PyDict_Update"><code class="xref c c-func docutils literal"><span class="pre">PyDict_Update()</span></code></a> doesn’t fall
302
back to the iterating over a sequence of key value pairs if the second
303
argument has no “keys” attribute. Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code> if an
304
exception was raised.</p>
305
<div class="versionadded">
306
<p><span class="versionmodified">New in version 2.2.</span></p>
310
<dl class="function">
311
<dt id="c.PyDict_MergeFromSeq2">
312
int <code class="descname">PyDict_MergeFromSeq2</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *a</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *seq2</em>, int<em> override</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_MergeFromSeq2" title="Permalink to this definition">¶</a></dt>
313
<dd><p>Update or merge into dictionary <em>a</em>, from the key-value pairs in <em>seq2</em>.
314
<em>seq2</em> must be an iterable object producing iterable objects of length 2,
315
viewed as key-value pairs. In case of duplicate keys, the last wins if
316
<em>override</em> is true, else the first wins. Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code>
317
if an exception was raised. Equivalent Python (except for the return
319
<div class="highlight-c"><div class="highlight"><pre><span class="n">def</span> <span class="n">PyDict_MergeFromSeq2</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">seq2</span><span class="p">,</span> <span class="n">override</span><span class="p">)</span><span class="o">:</span>
320
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="n">in</span> <span class="nl">seq2</span><span class="p">:</span>
321
<span class="k">if</span> <span class="n">override</span> <span class="n">or</span> <span class="n">key</span> <span class="n">not</span> <span class="n">in</span> <span class="nl">a</span><span class="p">:</span>
322
<span class="n">a</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
325
<div class="versionadded">
326
<p><span class="versionmodified">New in version 2.2.</span></p>
336
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
337
<div class="sphinxsidebarwrapper">
338
<h4>Previous topic</h4>
339
<p class="topless"><a href="list.html"
340
title="previous chapter">List Objects</a></p>
342
<p class="topless"><a href="class.html"
343
title="next chapter">Class and Instance Objects</a></p>
345
<ul class="this-page-menu">
346
<li><a href="../bugs.html">Report a Bug</a></li>
347
<li><a href="../_sources/c-api/dict.txt"
348
rel="nofollow">Show Source</a></li>
351
<div id="searchbox" style="display: none" role="search">
352
<h3>Quick search</h3>
353
<form class="search" action="../search.html" method="get">
354
<input type="text" name="q" />
355
<input type="submit" value="Go" />
356
<input type="hidden" name="check_keywords" value="yes" />
357
<input type="hidden" name="area" value="default" />
359
<p class="searchtip" style="font-size: 90%">
360
Enter search terms or a module, class or function name.
363
<script type="text/javascript">$('#searchbox').show(0);</script>
366
<div class="clearer"></div>
368
<div class="related" role="navigation" aria-label="related navigation">
371
<li class="right" style="margin-right: 10px">
372
<a href="../genindex.html" title="General Index"
375
<a href="../py-modindex.html" title="Python Module Index"
378
<a href="class.html" title="Class and Instance Objects"
381
<a href="list.html" title="List Objects"
383
<li><img src="../_static/py.png" alt=""
384
style="vertical-align: middle; margin-top: -1px"/></li>
385
<li><a href="https://www.python.org/">Python</a> »</li>
387
<span class="version_switcher_placeholder">2.7.11</span>
388
<a href="../index.html">Documentation</a> »
391
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> »</li>
392
<li class="nav-item nav-item-2"><a href="concrete.html" >Concrete Objects Layer</a> »</li>
396
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
398
The Python Software Foundation is a non-profit corporation.
399
<a href="https://www.python.org/psf/donations/">Please donate.</a>
401
Last updated on Jan 23, 2016.
402
<a href="../bugs.html">Found a bug</a>?
404
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'