[Python-ideas] Syntax for key-value iteration over mappings

Andrew Barnert abarnert at yahoo.com
Mon Jul 27 08:16:55 CEST 2015


On Jul 27, 2015, at 03:52, Rob Cliffe <rob.cliffe at btinternet.com> wrote:
> 
> One downside: Whatever implementation is chosen, it will not be "the one obvious way to do it".
> E.g. an .iteritems()-like implementation will fail if the dictionary is modified during the loop.
>        an .items()-like implementation will be expensive on a huge dictionary.

Why? The items method returns a view, an object that's backed by the dict itself. There is a bit of overhead, but it's constant, not linear on the dict size.

You may be thinking of 2.7, where items creates a list of pairs. In 3.x, it's equivalent to the 2.7 viewitems, not the 2.7 items.


More information about the Python-ideas mailing list