Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You asked: 'Are there any applicable differences between dict.items() and dict.iteritems()'</p> <p>This may help (for Python 2.x):</p> <pre><code>&gt;&gt;&gt; d={1:'one',2:'two',3:'three'} &gt;&gt;&gt; type(d.items()) &lt;type 'list'&gt; &gt;&gt;&gt; type(d.iteritems()) &lt;type 'dictionary-itemiterator'&gt; </code></pre> <p>You can see that <code>d.items()</code> returns a list of tuples of the key, value pairs and <code>d.iteritems()</code> returns a dictionary-itemiterator.</p> <p>As a list, d.items() is slice-able:</p> <pre><code>&gt;&gt;&gt; l1=d.items()[0] &gt;&gt;&gt; l1 (1, 'one') # an unordered value! </code></pre> <p>But would not have an <code>__iter__</code> method:</p> <pre><code>&gt;&gt;&gt; next(d.items()) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: list object is not an iterator </code></pre> <p>As an iterator, d.iteritems() is <strong>not</strong> slice-able:</p> <pre><code>&gt;&gt;&gt; i1=d.iteritems()[0] Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: 'dictionary-itemiterator' object is not subscriptable </code></pre> <p>But does have <code>__iter__</code>:</p> <pre><code>&gt;&gt;&gt; next(d.iteritems()) (1, 'one') # an unordered value! </code></pre> <p>So the items themselves are same -- the container delivering the items are different. One is a list, the other an iterator (depending on the Python version...) </p> <p>So the applicable differences between dict.items() and dict.iteritems() are the same as the applicable differences between a list and an iterator. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload