What is the difference between dict.items() and dict.iteritems() in Python2?
You asked: 'Are there any applicable differences between dict.items() and dict.iteritems()'
This may help (for Python 2.x):
>>> d={1:'one',2:'two',3:'three'}
>>> type(d.items())
<type 'list'>
>>> type(d.iteritems())
<type 'dictionary-itemiterator'>
You can see that d.items()
returns a list of tuples of the key, value pairs and d.iteritems()
returns a dictionary-itemiterator.
As a list, d.items() is slice-able:
>>> l1=d.items()[0]
>>> l1
(1, 'one') # an unordered value!
But would not have an __iter__
method:
>>> next(d.items())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list object is not an iterator
As an iterator, d.iteritems() is not slice-able:
>>> i1=d.iteritems()[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dictionary-itemiterator' object is not subscriptable
But does have __iter__
:
>>> next(d.iteritems())
(1, 'one') # an unordered value!
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...)
So the applicable differences between dict.items() and dict.iteritems() are the same as the applicable differences between a list and an iterator.
It's part of an evolution.
Originally, Python items()
built a real list of tuples and returned that. That could potentially take a lot of extra memory.
Then, generators were introduced to the language in general, and that method was reimplemented as an iterator-generator method named iteritems()
. The original remains for backwards compatibility.
One of Python 3’s changes is that items()
now return views, and a list
is never fully built. The iteritems()
method is also gone, since items()
in Python 3 works like viewitems()
in Python 2.7.
dict.items()
returns a list of 2-tuples ([(key, value), (key, value), ...]
), whereas dict.iteritems()
is a generator that yields 2-tuples. The former takes more space and time initially, but accessing each element is fast, whereas the second takes less space and time initially, but a bit more time in generating each element.
In Py2.x
The commands dict.items()
, dict.keys()
and dict.values()
return a copy of the dictionary's list of (k, v)
pair, keys and values.
This could take a lot of memory if the copied list is very large.
The commands dict.iteritems()
, dict.iterkeys()
and dict.itervalues()
return an iterator over the dictionary’s (k, v)
pair, keys and values.
The commands dict.viewitems()
, dict.viewkeys()
and dict.viewvalues()
return the view objects, which can reflect the dictionary's changes.
(I.e. if you del
an item or add a (k,v)
pair in the dictionary, the view object can automatically change at the same time.)
$ python2.7
>>> d = {'one':1, 'two':2}
>>> type(d.items())
<type 'list'>
>>> type(d.keys())
<type 'list'>
>>>
>>>
>>> type(d.iteritems())
<type 'dictionary-itemiterator'>
>>> type(d.iterkeys())
<type 'dictionary-keyiterator'>
>>>
>>>
>>> type(d.viewitems())
<type 'dict_items'>
>>> type(d.viewkeys())
<type 'dict_keys'>
While in Py3.x
In Py3.x, things are more clean, since there are only dict.items()
, dict.keys()
and dict.values()
available, which return the view objects just as dict.viewitems()
in Py2.x did.
But
Just as @lvc noted, view object isn't the same as iterator, so if you want to return an iterator in Py3.x, you could use iter(dictview)
:
$ python3.3
>>> d = {'one':'1', 'two':'2'}
>>> type(d.items())
<class 'dict_items'>
>>>
>>> type(d.keys())
<class 'dict_keys'>
>>>
>>>
>>> ii = iter(d.items())
>>> type(ii)
<class 'dict_itemiterator'>
>>>
>>> ik = iter(d.keys())
>>> type(ik)
<class 'dict_keyiterator'>