[Python-ideas] Iteritems() function?

Masklinn masklinn at masklinn.net
Wed Jun 8 13:25:42 CEST 2011

On 2011-06-08, at 13:01 , Jan Kaliszewski wrote:
> But sometimes it may be a dict {first: second, ...} OR a seq [(first,
> second), ...] and in fact we are not interested in it -- we simply want
> to iterate over its items... But we are forced to do type/interface
> check, e.g.:
>    if isinstance(coll, collections.Mapping):
>        for first, second in some_items.items(): ...
>    else:
>        for first, second in some_items: …
You could just convert everything to a dict:

    for first, second in dict(some_items).iteritems():
        # etc…

More information about the Python-ideas mailing list