1. What is the difference between dict.items() and dict.iteritems()?
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 a iterator-generator method named iteritems()
. The original being left for backwards compatibility. One of the changes of Python 3.x is that theitems()
methods now also return iterators and a list is never fully built. In Python 3.x there is noiteritems()
method any longer,asitems()
is now the same asiteritems()
in Python 2.x (WRONG!!!).
the Py3 behavior isn't the same as iteritems()
. It actually makes a full sequence-protocol object that also reflects changes to the dict (and is backed by the dict itself, rather than a redundant list)- it's been backported to 2.7 as viewitems()
In Py2.x :
dict.items()
, dict.keys()
and dict.values()
return acopy of the dictionary'slist of(k, v)
pair, keys and values, which could takes a lot of memory if the copied list is very large.
dict.iteritems()
, dict.iterkeys()
and dict.itervalues()
return aniterator over the dictionary’s(k, v)
pair, keys and values
dict.viewitems()
, dict.viewkeys()
and dict.viewvalues()
return theview objects, which can reflect the dictionary's changes (i.e. if youdel
an item or add a(k,v)
pair in the dictionary, the view object canautomatically change at the same time.)
While in Py3.x, things are more clean, since there are only
dict.items()
, dict.keys()
and dict.values()
available, which return theview objects just asdict.viewitems()
in Py2.x did.
But just as @lvc noted, view object isn't the same asiterator, so if you want to return aniterator in Py3.x, you could useiter(dictview)
:
2. ImportError: No module named matplotlib.pyplot
You have 2 pythons installed on your machine, one is the standard python that comes with MacOSX and the second is the one you installed with ports (this is the one that has matplotlib
installed in it's library, the one that comes with macosx does not).
/usr/bin/python
Is the standard mac python and since it doesn't have matplotlib
you should always start your script with the one installed with ports.